I'm looking for a LaTeX code sniffer and/or code beautifier, because I'm creating a CI server for my LaTeX books and I wanted a few things. :D
5 Answers
latexindent.pl
is available onctan
and is part oftexlive
You can find (and follow) the project on github
at
https://github.com/cmhughes/latexindent.pl
basic usage
You can run latexindent
from the command line by typing one of the following, depending on your operating system and tex distribution
latexindent myfile.tex
latexindent.pl myfile.tex
latexindent.exe myfile.tex
You can also run the script by using the arara
rule, indent.yaml
.
example 1: getting started
By default every time the script comes
across \begin{<something>}...end{<something>}
it will increase the indentation.
Starting with the following in mwe.tex
mwe.tex
\begin{figure}[!htb]
\centering
\begin{tikzpicture}
\begin{axis}[
framed,
width=\figurewidth,
xmin=-5,xmax=5,
ymin=-1,ymax=5,
xtick={-6},
ytick={-6},
]
\addplot expression[domain=-4.5:2.2]{2^x};
\end{axis}
\end{tikzpicture}
\end{figure}
and running
latexindent mwe.tex
gives the following output:
\begin{figure}[!htb]
\centering
\begin{tikzpicture}
\begin{axis}[
framed,
width=\figurewidth,
xmin=-5,xmax=5,
ymin=-1,ymax=5,
xtick={-6},
ytick={-6},
]
\addplot expression[domain=-4.5:2.2]{2^x};
\end{axis}
\end{tikzpicture}
\end{figure}
example 2: align delimiters
latexindent.pl
will aim to align delimiter-based environments; for example, starting with the following in mwe.tex
\begin{align*}
F(-x) & =-(-x)^2 & G(-x) & =-(-x)^4 & H(-x) & =-(-x)^6 \\
& =-x^2 & & =-x^4 & & =-x^6\\
& =F(x) & & =G(x) & & =H(x)
\end{align*}
and running
latexindent.pl mwe.tex
gives
\begin{align*}
F(-x) & =-(-x)^2 & G(-x) & =-(-x)^4 & H(-x) & =-(-x)^6 \\
& =-x^2 & & =-x^4 & & =-x^6 \\
& =F(x) & & =G(x) & & =H(x)
\end{align*}
customising the indentation
All of the customisation is done by using the YAML
interface. To get started, you might try creating the following small file
localSettings.yaml
defaultIndent: " "
and then run
latexindent.pl -l mwe.tex
If you'd prefer to name the YAML
file something else (say, myfile.yaml
) then you can run
latexindent.pl -l=myfile.yaml mwe.tex
some further things to explore
The documentation in pdf
or html
contains full details. Some things that you might like to explore:
verbatimEnvironments
for verbatim-based environmentsspecialBeginEnd
for your own patterns- the
-m
switch which allows you to modify line breaks
before using it on anything important...
I recommend both using the following:
- a visual check
- a check using
latexdiff inputfile.tex outputfile.tex
-
Hi cmhughes, very interesting idea. But I get no difference in my source file and the resulting output. Indeed, when I run your script on
sampleBEFORE.tex
I still get no changes. (Perl ver. 5.12.4, running on OS X 10.8.1.)– NVaughanSep 11, 2012 at 1:41 -
Would you like updating the answer: informing the visitor that latexindent is part of TexLive. So users having TexLive can execute it directly without having to download from CTAN or following github readme.– azzamsaApr 6, 2019 at 3:24
I created a website that reformats the latex code to make indents consistent. This uses latexindent in the backend. This if a very rudimentary system but will do all simple indentation of items inside parentheses so they are at the same level.
It does not do any code sniffing, it also cannot detect if you have missing end tags.
-
1
-
-
1Yes It does use latexindent. I am working to implement all the features of the application.– WhitecatNov 13, 2018 at 15:05
-
1@Whitecat I love your website and use it almost daily to reshape my code and fin my errors. One question, is it normal that it cannot be used sometimes (as if your site was off) . And then it comes back... Merci !– JeTMay 3, 2020 at 17:51
-
I need to make a more dedicated site as that one is hosted with some other word press sites which often get DDosed. I also made latexformat.com which looks more pretty.– WhitecatMay 4, 2020 at 15:04
The closest thing to a LaTeX code sniffer is the nag
package which tries to detect and warn about usage of obsolete package and other things listed in l2tabu
.
I'm not aware that a (La)TeX beautifier exists. LaTeX is a TeX format and TeX is a very dynamic language which allows to change its own parser rules during the compilation runs. This makes it very hard to process except by a full TeX compiler itself. I was thinking writing a limited tool using Perl a while ago, but never found the time to do so.
There are several (very) simple code tidiers available.
Some come with editors (e.g. Textmate has a LaTeX Tidy function) but I don't have an OS that can run this and it may just be the product below (see its edit history).
Some are written by users who needed one and wrote their own and have made it more widely available, e.g. Eric Tsu's LateX Tidy. I have used Eric Tsu's program a couple of times to reformat received LaTeX code as an aid to adapting it (to shorten lines, to remove redundant blank lines and to indent blocks (i.e. \begin
environment) but, as with most products, I wouldn't use it to format code without subsequent detailed checking -- as it says on the website
This is a little Perl script to neaten up the format of LaTeX files.
This is simple and naive. This takes STDIN .tex files and prints to STDOUT.
Check your file! Backup! No guarantees!
License
This is released as Niceware, which is like the Perl Artistic License, except you have to be nice to me when you criticize the code.
If you are looking for something more than tools like this let us know if you find it -- or end up writing it yourself.
VSCode has an extension called Prettier which autoformats your (latex) code. After installing it in the VSCode IDE, one can autoformat a .tex
file by pressing:
Ctrl+Shift+P
, Format Document
, <Press enter>
. (On mac replace Ctrl
with CMD
).
You can try it out on a .tex
file online on their website before installing it.
Note
I would be interested in seeing an example .yml
that shows how prettier can be included in(to GitLab) CI, as a check/test. Basically ensuring a build error is thrown if not all .tex
files in a repository are prettier
requirement compliant, and getting a build pass if the .tex
files are formatted as prettier
dictates. Please share a link to such explanation, or include it as a new answer, as I believe that may still be within the scope of the question.
latexindent
is now a part ofTeXLive 2013