87

In ${\rm\LaTeX}$ typesetting, when we repeat a long and complex formula in long documents, it is appropriate to create a new command that just by calling this new command we get the desired output. For example, I have used the following math expression in my previous document frequently: $$\{a^1,a^2,\ldots,a^n\}$$ For doing this in usual way, we need to press 22 keys on keyboard (and think about $(\frac{\partial}{\partial x^1}, \cdots,\frac{\partial}{\partial x^n})$ and other terrible formulas). Of course we can do this by copy and paste from similar one in the text. it is much better to define the following new command on preamble

\newcommand{\set}[1]{\setaux#1\relax}
\def\setaux#1#2#3\relax{%
  \{ {#1}#2 1,
  \ifnum\pdfstrcmp{#3}{3}=0
    {#1}#2 2
  \else
    \ldots
  \fi
  , {#1}#2{#3} \}
}

and just by typing \set{a^n} in our text we get the same output.

Question: What are your favorite ${\rm\LaTeX}$ tricks that save your time in long document typesetting?

C.F.G
  • 4,165
  • 5
  • 30
  • 64
  • 11
    I would save time in your example by typing out the lists directly, since your preamble code has way more than 22 keys. Maybe I'd just make special code for partial derivatives. – KConrad Jan 03 '19 at 06:28
  • @KConrad, thanks for your precision. where we use \ldots and \cdots? – C.F.G Jan 03 '19 at 06:51
  • 6
    @C.F.G.: \ldots when the surrounding symbols are aligned at the bottom of the line: commas, for example. \cdots when the symbols align centered: infix operational symbols, colons, etc. So, $a_1+\cdots+a_n$, with \cdots, but $a_1,\ldots,a_n$, with \ldots. Compare with $a_1+\ldots+a_n$ and $a_1,\cdots,a_n$. – Arturo Magidin Jan 03 '19 at 07:14
  • 1
  • 2
    For what it's worth, rather than \ldots and \cdots one should probably use semantic commands like a_1, \dotsc, a_n and a_1 + \dotsb + a_n. (This comment basically duplicates @‍user49915's comment on the post linked by @MartinSleziak.) – LSpice Jan 03 '19 at 08:19
  • 36
    There is a TeX stackexchange site where this sort of thing should be discussed. – Andrej Bauer Jan 03 '19 at 09:02
  • 4
    There may be LaTeX shortcuts especially useful to mathematicians (e.g. ArXiv2Bib) so I think this question could stay here. (We may also give reference to some stackexchange pages if there are useful general shortcuts.) – François Brunault Jan 03 '19 at 09:22
  • My favorite macro is \tup defined as \left\langle#1\right\rangle, along with another command to create a \middle| with spacing. It's useful for ordered pairs, and sequences, and those are very common in my work so far. – Asaf Karagila Jan 03 '19 at 09:26
  • 7
    @AsafKaragila You can use \usepackage{mathtools} and \DeclarePairedDelimiter{\tup}{\langle}{\rangle}, instead. In this way, you also get a \tup* command that scales delimiters for free, and it may include slightly better spacing. I use this normally for \DeclarePairedDelimiter{\norm}{\lVert}{\rVert}. – Federico Poloni Jan 03 '19 at 10:06
  • Having to define a new latex command is also a good hint that it might be worth to define a new symbol/object in the paper/proof itself. – Artem Kaznatcheev Jan 03 '19 at 14:43
  • @AsafKaragila: Funny, my \tup{...} is \left( ... \right). It says something if we mathematicians can't even agree on how to write tuples :) – darij grinberg Jan 03 '19 at 16:02
  • 3
    If you are keen on this question not being appropriate, it should rather be migrated, not merely closed. – Dima Pasechnik Jan 04 '19 at 04:07

14 Answers14

116

Related to the question. I showed the following web-pages to a table-neighbor at a conference and literally got the "You just saved 3 days of my life" reaction.

Doi2Bib ISBN2Bib Arxiv2Bib

These web-sites are exactly as described: given say a DOI, it produces the bibtex entry for that particular article. I use this all the time.

  • 13
    I would upvote this every three seconds for three hours in a row if I could :) – მამუკა ჯიბლაძე Jan 03 '19 at 06:38
  • 9
    It is also easy to get bibtex from mrlookup. https://mathscinet.ams.org/mrlookup – Russ Woodroofe Jan 03 '19 at 08:49
  • 36
    My experience is that the bibtex files you get from Mathscinet are of better quality than anything else around, when it comes to correct capitalization/diacritics/formulas and consistent journal name abbreviations. Everything else seems to require some manual tweaking. – Federico Poloni Jan 03 '19 at 10:11
  • 7
    If you use MathSciNet for bibtex references, you might find https://github.com/jhpalmieri/bibweb helpful: it is a perl script that looks up references from the command-line rather than from a web browser. – John Palmieri Jan 08 '19 at 16:19
  • I would like to also suggest Google Scholar: under each result there is a double quotation icon which you can click to get a bibtex entry. It's not perfect though and indeed it sometimes requires manual tweaking. – user2831784 Nov 06 '19 at 09:35
  • JabRef also allows you get the bibtex code from DOI, ISBN, arXiv identifier as well as from MathSciNet and Zentralblatt. You find the option to receive an article by their id under "Library > New entry" in the lower part of the dialog. – Tobias Diez Mar 22 '20 at 20:56
  • 1
    Is there any way to export bib file of many titles/doi by one click? e.g. entering 10 paper name and exporting bib file of all of them into one single file. – C.F.G Jun 16 '22 at 07:19
62

In case you want to use tikzcd to draw diagrams this tikzcd editor saves lot of time.

\begin{tikzcd}  &  & A \arrow[rr] \arrow[lld] \arrow[dd] &  & V \arrow[d] &  \arrow[dd] \\ 
 & V \arrow[dd] \arrow[ru] \arrow[ldd] &  \arrow[rru] &  \arrow[d] & V &  \\
  &  & V \arrow[rru] &  &  \arrow[ru] \arrow[lu] \arrow[l] &  \\ 
 & V \arrow[ru] \arrow[rr] &  &  &  &  \end{tikzcd}

In some sense, I have written above code in less than 1 minute.

It is self-explanatory how to draw diagrams.

In bottom of the page, you have an option to copy the code.

45

To give another answer: The package cleveref should almost be mandatory.

Referring to a label, one can use \cref{thelabel} to produce things like Lemma 2, Theorem 5, etc where "Lemma" and "Theorem" are deduced automatically from the object with the label.

Thus, changing a proposition to a theorem, or a section to a subsection, is pain-free, as all instances of Proposition 5 becomes Theorem 5. This saves a lot of time.

  • 2
    Output of \cref{thelabel} is things like 'lemma 2' and for \Cref{thelabel} is 'Lemma 2`. – C.F.G Jan 08 '19 at 04:31
  • 1
    @C.F.G Sure, or one can change some options to make it capitalize even with \cref{...} – Per Alexandersson Jan 08 '19 at 06:44
  • 19
    Contrary argument: anything with 'clever' in the title, at least if you didn't make it yourself, will break unexpectedly and in ways that are difficult to fix. (Addendum: if you did make it yourself, it will still break unexpectedly and in ways that are difficult to fix, only now there will be the additional element of shame that you can no longer remember how to handle the cleverness that went into the original creation.) – LSpice Jan 09 '19 at 14:36
  • @LSpice A package by any other name would smell as clever :) – Per Alexandersson Jan 09 '19 at 17:20
  • 1
    autoref (included in the hyperref package) also does this, although it takes some fidgeting to get different autoref names if you use the same counter. – R. van Dobben de Bruyn Apr 25 '20 at 05:46
  • 5
    To back-up the comment by @LSpice: One instance where it sometimes (but not always) breaks down is in languages that require flexion of nouns in different grammatical cases. (In a recent discussion, a colleague told me that there is an implementation of cleverref for the German language, and he was a bit surprised when I replied that I prefer technology that does not prevent me from using the genitive case...) – Jochen Glueck Jul 11 '20 at 22:38
  • This is really useful answer (at least to me).. – Praphulla Koushik May 18 '21 at 20:29
16

I like the package cells by Sergei Ivanov, which allows to write

enter image description here

to produce enter image description here

Fedor Petrov
  • 102,548
15

Mathpix is a nice tool to convert images to LaTeX: "Take a screenshot of math and paste the LaTeX into your editor, all with a single keyboard shortcut". Formulae can be taken from Internet browser, djvu-, pdf-, ... documents and even from hand-written notes.

Unfortunately now it allows to get for free only 50 formulae per month.

UPD: My antivirus killed it.

enter image description here

15

I'm surprised the inputenc package isn't more widely known. Simply by putting

\usepackage[utf8]{inputenc}

into your preamble, you can directly write diacritics in your .tex file, e.g. Poincaré instead of Poincar\'{e}, Möbius instead of M\"{o}bius, and so forth.

Arun Debray
  • 6,766
  • 1
    This is nice. If only it helped with Cyrillic, Georgian, Korean etc. – Robert Furber Nov 11 '19 at 18:02
  • 10
    For more than a year now, LaTeX default input encoding scheme has been utf8. Hence, running \usepackage[utf8]{inputenc} no longer does anything special -- unless one's TeX distribution hasn't been updated in a long time. Running \usepackage{T1]{fontenc}, on the other hand, is still a good idea if one's document features more the most basic accented characters (such as é and è). – Mico Mar 31 '20 at 04:41
14

I'm not sure but new commands can be inconvenient for the editors. So the long plain commands can be better in this case. In order to save time I use macros which replace some simple strings by usual $\LaTeX$ commands and put cursor at appropriate position. Simple examples are \begin{align*} ...&\to\verb"\ldots"\\ \verb"//"&\to\verb"\frac{}{}"\\ \verb"`a"&\to\verb"\alpha"\\ \verb"ZZZ"&\to\verb"\mathbb{Z}"\\ \end{align*}

  • 18
    New commands can also be dangerous if the typesetters don't handle them properly. I know of a paper where the author apparently did \renewcommand{\lll}{\mathcal{L}} to save typing. This apparently got dropped in editing, and the printed version had every instance of $\mathcal{L}$ replaced by $\lll$, making it look nonsensical. I don't know why it wasn't caught when the proofs were corrected, but still, it's a cautionary tale. – Nate Eldredge Jan 07 '19 at 15:34
  • 1
    @Nate Eldredge Wait, why would any command be dropped in editing? Why would new commands introduce any inconvenience? It seems very frustrating to forgo the many benefits of new commands when they were introduced for a reason. – silvascientist May 24 '19 at 16:44
  • 6
    @silvascientist: I actually don't know firsthand how the error occurred in the paper I mentioned; my description is my best guess. I suspect the "inconvenience" arises when the editorial staff has to manually edit your LaTeX source (which is not unusual) and isn't familiar with your new commands, or tries to introduce their own new commands (e.g. from a house style file) which conflict. – Nate Eldredge May 24 '19 at 18:38
  • 9
    General advice I've heard is that \newcommand is fine but \renewcommand is dangerous since you don't know where the original version of the command might be used in style files that get applied later. – Greg Friedman May 29 '19 at 02:26
12

As noted by Alexey Ustinov, for portability any custom definitions may be better avoided. I find TextExpander very convenient to achieve the same functionality (a brief command that expands in a longer piece of LaTeX code) without a custom definition. In this connection I might note another functionality offered by TextExpander, which is to use LaTeX commands in any text field (chat, email, Slack, etc.) and expand symbols into their Unicode characters. The macros for this can be downloaded from the TextExpander site. You can specify which applications should use the Unicode expansions and which should leave the bare LaTeX command, so that this approach does not interfere with the usual work flow when an application understands LaTeX.

Carlo Beenakker
  • 177,695
  • 1
    +1. I posted a related question on the TEX.Stack.exchange, and you provided another answer. – Daniele Tampieri Jan 03 '19 at 13:16
  • 3
    FYI, the basic TextExpander syntax, for example for the equation environment, is \begin{equation}%\end{equation} where % positions the cursor so you can enter the equation; I assign the shortcut /eq to this expansion, and enable it only in the TeX application, so conflicts with other text fields are unlikely. TextExpander is indispensable for my work flow. – Carlo Beenakker Jan 03 '19 at 13:44
12

Take a look at this repo called tikzplotlib, it can convert plots that are generated by Python using matplotlib to tikz graphs. For example, I can draw complex plots using Python, and call the function tikz_save and save it as a .tex file, and use input{<filename>.tex} to include it in the $\LaTeX\space$ document. The graphs are prettier and more customizable.

It definitely saved me three days doing my Calculus homework

Update: Like @Horror Vacui said, now tikzplotlib can convert to pgfplots, and it's easier to plot pgfplots directly in latex.

At the time when I first posted this answer, this package is still called matplotlib2tikz and it didn't support conversion to pgfplots.

tjysdsg
  • 101
  • Borrowing from another answer: "you saved 3 days of my life" :D – Taladris Apr 09 '20 at 14:38
  • Why not just simply saving the matplotlib plots into pgf, which can be plotted in latex? Adding tikz into the chain seems to be an overhead. It makes sense only if you use tikz for other figures. – Horror Vacui Jul 13 '20 at 13:50
9

I found Python's option to generate \LaTeX output from symbolic calculations quite useful. One specific usecase was to calculate higher derivatives of rotated explicit functions, which I calculated with SymPy:

import sympy
from sympy.printing import latex as spl

sympy.init_printing()
t = sympy.Symbol("t")
r = sympy.Symbol("r")
f = sympy.Function("f")(t)        # the explicit function f(t)
x = sympy.cos(r)*t-sympy.sin(r)*f # x-coordinate after rotation 
y = sympy.sin(r)*t+sympy.cos(r)*f # y-coordinate after rotation
# 1st derivative w.r.t. x after rotation
dy = sympy.Derivative(y,t)
dx = sympy.Derivative(x,t)
d1 = dy.doit()/dx.doit()
#  2nd derivative w.r.t. x after rotation
d2 = sympy.Derivative(d1,t)
d2 = d2.doit()/dx.doit()
d2 = sympy.trigsimp(d2)
#  3rd derivative w.r.t. x after rotation
d3 = sympy.Derivative(d2,t)
d3 = d3.doit()/dx.doit()
d3 = sympy.trigsimp(d3)
#  4th derivative w.r.t. x after rotation
d4 = sympy.Derivative(d3,t)
d4 = d4.doit()/dx.doit()
d4 = sympy.trigsimp(d4)
d4 = d4.collect(sympy.cos(r)).collect(sympy.sin(r)).collect(sympy.cos(2*r)).collect(sympy.sin(2*r))   


## generate \LaTex code in e.g. IPython console:
In [1]: spl(d1)
Out[1]: '\\frac{\\sin{\\left (r \\right )} + \\cos{\\left (r \\right )} \\frac{d}{d t} f{\\left (t \\right )}}{- \\sin{\\left (r \\right )} \\frac{d}{d t} f{\\left (t \\right )} + \\cos{\\left (r \\right )}}'
In [2]: spl(d2)
Out[2]: '- \\frac{\\frac{d^{2}}{d t^{2}}  f{\\left (t \\right )}}{\\left(\\sin{\\left (r \\right )} \\frac{d}{d t} f{\\left (t \\right )} - \\cos{\\left (r \\right )}\\right)^{3}}'
In [3]: spl(d3)
Out[3]: '\\frac{1}{\\left(\\sin{\\left (r \\right )} \\frac{d}{d t} f{\\left (t \\right )} - \\cos{\\left (r \\right )}\\right)^{5}} \\left(\\sin{\\left (r \\right )} \\frac{d}{d t} f{\\left (t \\right )} \\frac{d^{3}}{d t^{3}}  f{\\left (t \\right )} - 3 \\sin{\\left (r \\right )} \\left(\\frac{d^{2}}{d t^{2}}  f{\\left (t \\right )}\\right)^{2} - \\cos{\\left (r \\right )} \\frac{d^{3}}{d t^{3}}  f{\\left (t \\right )}\\right)'
In [4]: spl(d4)
Out[4]: '\\frac{1}{\\left(\\sin{\\left (r \\right )} \\frac{d}{d t} f{\\left (t \\right )} - \\cos{\\left (r \\right )}\\right)^{7}} \\left(\\left(\\frac{d}{d t} f{\\left (t \\right )} \\frac{d^{4}}{d t^{4}}  f{\\left (t \\right )} - 5 \\frac{d^{2}}{d t^{2}}  f{\\left (t \\right )} \\frac{d^{3}}{d t^{3}}  f{\\left (t \\right )}\\right) \\sin{\\left (2 r \\right )} + \\left(\\frac{1}{2} \\left(\\frac{d}{d t} f{\\left (t \\right )}\\right)^{2} \\frac{d^{4}}{d t^{4}}  f{\\left (t \\right )} - 5 \\frac{d}{d t} f{\\left (t \\right )} \\frac{d^{2}}{d t^{2}}  f{\\left (t \\right )} \\frac{d^{3}}{d t^{3}}  f{\\left (t \\right )} + \\frac{15}{2} \\left(\\frac{d^{2}}{d t^{2}}  f{\\left (t \\right )}\\right)^{3} - \\frac{1}{2} \\frac{d^{4}}{d t^{4}}  f{\\left (t \\right )}\\right) \\cos{\\left (2 r \\right )} - \\frac{1}{2} \\left(\\frac{d}{d t} f{\\left (t \\right )}\\right)^{2} \\frac{d^{4}}{d t^{4}}  f{\\left (t \\right )} + 5 \\frac{d}{d t} f{\\left (t \\right )} \\frac{d^{2}}{d t^{2}}  f{\\left (t \\right )} \\frac{d^{3}}{d t^{3}}  f{\\left (t \\right )} - \\frac{15}{2} \\left(\\frac{d^{2}}{d t^{2}}  f{\\left (t \\right )}\\right)^{3} - \\frac{1}{2} \\frac{d^{4}}{d t^{4}}  f{\\left (t \\right )}\\right)'  

so, what remains to be done is to replace double backslashes by single ones and to provide the \$ signs

Manfred Weis
  • 12,594
  • 4
  • 34
  • 71
9

Commands like \pd{f}{x} (= $\frac{\partial f}{\partial x}$) defined by \newcommand\pd[2]{\frac{\partial #1}{\partial #2}} is very useful. It would be better if there is a generalized command of this like \pd{f}{x}{y}{z}... for $\frac{\partial^k f}{\partial x\partial y\partial z...}$ and add it to amspackage.

Edit (after comment of @pbelmans): Such commands are avalible in diffcoeff package.

C.F.G
  • 4,165
  • 5
  • 30
  • 64
6

Detexify http://detexify.kirelabs.org/classify.html allows your to draw the symbol you want and then it will output a list of the most probable latex commands for that symbol.

5

A really useful website is GroupNames created by Tim Dokchitser https://people.maths.bris.ac.uk/~matyd/GroupNames/

It lists all finite groups of order $n$ with $n \leq 500$, with the exception of $n=256,384$, and has great search features. When you click on a group, you are given all sorts of useful information about that group (this is often way quicker than using a computational algebra system such as Magma or Sage). In particular, there is the option to export the latex code for the subgroup lattice and the character table. The latter saved me a ton of time when teaching a course on representation theory of finite groups!

4

For $\LaTeX$ typing, i create an online tool (link : https://latexeditor.lagrida.com/) with code examples, and every time i need a code i juste click on examples :

enter image description here

enter image description here

I can also write formulas very easly by few clicks:

enter image description here