89
$\begingroup$

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?

$\endgroup$
12
  • 11
    $\begingroup$ 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. $\endgroup$
    – KConrad
    Commented Jan 3, 2019 at 6:28
  • 6
    $\begingroup$ @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$. $\endgroup$ Commented Jan 3, 2019 at 7:14
  • 36
    $\begingroup$ There is a TeX stackexchange site where this sort of thing should be discussed. $\endgroup$ Commented Jan 3, 2019 at 9:02
  • 4
    $\begingroup$ 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.) $\endgroup$ Commented Jan 3, 2019 at 9:22
  • 7
    $\begingroup$ @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}. $\endgroup$ Commented Jan 3, 2019 at 10:06

14 Answers 14

116
$\begingroup$

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.

$\endgroup$
7
  • 13
    $\begingroup$ I would upvote this every three seconds for three hours in a row if I could :) $\endgroup$ Commented Jan 3, 2019 at 6:38
  • 9
    $\begingroup$ It is also easy to get bibtex from mrlookup. mathscinet.ams.org/mrlookup $\endgroup$ Commented Jan 3, 2019 at 8:49
  • 36
    $\begingroup$ 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. $\endgroup$ Commented Jan 3, 2019 at 10:11
  • 7
    $\begingroup$ If you use MathSciNet for bibtex references, you might find github.com/jhpalmieri/bibweb helpful: it is a perl script that looks up references from the command-line rather than from a web browser. $\endgroup$ Commented Jan 8, 2019 at 16:19
  • 1
    $\begingroup$ 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. $\endgroup$
    – C.F.G
    Commented Jun 16, 2022 at 7:19
62
$\begingroup$

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.

$\endgroup$
1
  • $\begingroup$ Yes! That's definitely been missing for too long! $\endgroup$ Commented Jul 14, 2020 at 8:07
45
$\begingroup$

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.

$\endgroup$
7
  • 2
    $\begingroup$ Output of \cref{thelabel} is things like 'lemma 2' and for \Cref{thelabel} is 'Lemma 2`. $\endgroup$
    – C.F.G
    Commented Jan 8, 2019 at 4:31
  • 1
    $\begingroup$ @C.F.G Sure, or one can change some options to make it capitalize even with \cref{...} $\endgroup$ Commented Jan 8, 2019 at 6:44
  • 19
    $\begingroup$ 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.) $\endgroup$
    – LSpice
    Commented Jan 9, 2019 at 14:36
  • 1
    $\begingroup$ 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. $\endgroup$ Commented Apr 25, 2020 at 5:46
  • 5
    $\begingroup$ 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...) $\endgroup$ Commented Jul 11, 2020 at 22:38
16
$\begingroup$

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

enter image description here

to produce enter image description here

$\endgroup$
3
15
$\begingroup$

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.

$\endgroup$
2
  • 1
    $\begingroup$ This is nice. If only it helped with Cyrillic, Georgian, Korean etc. $\endgroup$ Commented Nov 11, 2019 at 18:02
  • 10
    $\begingroup$ 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 è). $\endgroup$
    – Mico
    Commented Mar 31, 2020 at 4:41
15
$\begingroup$

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

$\endgroup$
4
  • 6
    $\begingroup$ I just tried it and that's great (although I guess the obvious compositions are not the identity maps). With this I may also find out convenient LaTeX commands which I didn't know. $\endgroup$ Commented Apr 29, 2019 at 10:49
  • 1
    $\begingroup$ Now, this app, isn't free. $\endgroup$
    – C.F.G
    Commented Nov 11, 2019 at 5:09
  • 1
    $\begingroup$ @C.F.G Unfortunately yes. It allows to get for free only 50 formulae per month. $\endgroup$ Commented Nov 11, 2019 at 6:08
  • 5
    $\begingroup$ For finding a specific LaTeX symbol, Detexify (detexify.kirelabs.org/classify.html) is a useful website. $\endgroup$ Commented Jul 14, 2020 at 2:53
14
$\begingroup$

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*}

$\endgroup$
4
  • 18
    $\begingroup$ 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. $\endgroup$ Commented Jan 7, 2019 at 15:34
  • 1
    $\begingroup$ @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. $\endgroup$ Commented May 24, 2019 at 16:44
  • 6
    $\begingroup$ @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. $\endgroup$ Commented May 24, 2019 at 18:38
  • 9
    $\begingroup$ 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. $\endgroup$ Commented May 29, 2019 at 2:26
12
$\begingroup$

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.

$\endgroup$
2
  • 1
    $\begingroup$ +1. I posted a related question on the TEX.Stack.exchange, and you provided another answer. $\endgroup$ Commented Jan 3, 2019 at 13:16
  • 3
    $\begingroup$ 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. $\endgroup$ Commented Jan 3, 2019 at 13:44
12
$\begingroup$

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.

$\endgroup$
2
  • $\begingroup$ Borrowing from another answer: "you saved 3 days of my life" :D $\endgroup$
    – Taladris
    Commented Apr 9, 2020 at 14:38
  • $\begingroup$ 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. $\endgroup$ Commented Jul 13, 2020 at 13:50
9
$\begingroup$

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

$\endgroup$
2
  • 7
    $\begingroup$ If you replace spl(d1) with print(spl(d1)), you get directly the version with single backslashes. $\endgroup$ Commented Jan 3, 2019 at 10:04
  • 5
    $\begingroup$ In the same vein, Mathematica has the TeXForm function $\endgroup$ Commented Jan 3, 2019 at 14:32
9
$\begingroup$

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.

$\endgroup$
5
  • 7
    $\begingroup$ There is a package for this: ctan.org/pkg/diffcoeff. $\endgroup$
    – pbelmans
    Commented Jan 8, 2019 at 7:54
  • 1
    $\begingroup$ @pbelmans: thank you for this package. why it is not part of amspackage? $\endgroup$
    – C.F.G
    Commented Jan 8, 2019 at 7:59
  • 9
    $\begingroup$ Because not every math-related thing is part of amsmath, to not make it bloated I guess. $\endgroup$
    – pbelmans
    Commented Jan 8, 2019 at 8:14
  • 3
    $\begingroup$ The cool, bropd, commath, esdiff, or physymb packages also provide similar commands, with additional functionality for higher derivatives (see ctan.org). $\endgroup$
    – Ben McKay
    Commented Jan 8, 2019 at 8:56
  • 1
    $\begingroup$ The physics package provides similar functionality. $\endgroup$
    – oliversm
    Commented Jul 12, 2020 at 8:34
6
$\begingroup$

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.

$\endgroup$
5
$\begingroup$

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!

$\endgroup$
4
$\begingroup$

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

$\endgroup$

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .