83

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

MasonX
  • 831

5 Answers5

73

latexindent.pl is available on ctan and is part of texlive

See the documentation in pdf or html for full details.

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 environments
  • specialBeginEnd 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
cmhughes
  • 100,947
  • 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.) – NVaughan Sep 11 '12 at 01: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. – azzamsa Apr 06 '19 at 03:24
19

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.

https://c.albert-thompson.com/latex-pretty/

Whitecat
  • 1,321
  • 1
    Works great! Thanks for providing this service. – Janosh Jul 21 '16 at 03:01
  • Does your site use latexindent? – cmhughes Nov 09 '18 at 21:55
  • 1
    Yes It does use latexindent. I am working to implement all the features of the application. – Whitecat Nov 13 '18 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 ! – JeT May 03 '20 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 http://latexformat.com/ which looks more pretty. – Whitecat May 04 '20 at 15:04
14

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.

Martin Scharrer
  • 262,582
9

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.

mas
  • 5,949
1

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).

Illustration

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.

a.t.
  • 267