65

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

  • 2
    When you say "CI server", do you mean continuous integration? Could you expand on that? I find the question confusing. – Matthew Leingang Aug 19 '11 at 15:46
  • 3
    latexindent is now a part of TeXLive 2013 – cmhughes Nov 26 '13 at 18:07
  • I created a website that reformats the latex code to make indents correct. c.albert-thompson.com/latex-pretty – Whitecat Oct 5 '15 at 6:32
59

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
  • 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 1:41
  • @NVaughan I'm sorry to hear that :) Try and catch me in the chat room sometime and we can talk about debugging; running perl indent.plx sampleBEFORE.tex> sampleAFTER.tex should show you it in action. – cmhughes Sep 11 '12 at 2:58
  • 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 6 at 3:24
  • @azzamsa thanks, I've updated the answer :) feel free to make other changes if you'd like – cmhughes Apr 6 at 8:40
11

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.

  • Thanks for pointing to nag and l2tabu. +1 – guillem Mar 14 '13 at 6:38
9

I created a website that reformats the latex code to make indents consistent. This if a very rudimentary system but will do all simple indentation of items inside parenthases 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/

  • Works great! Thanks for providing this service. – Casimir Jul 21 '16 at 3:01
  • Does your site use latexindent? – cmhughes Nov 9 '18 at 21:55
  • Yes It does use latexindent. I am working to implement all the features of the application. – Whitecat Nov 13 '18 at 15:05
7

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.

  • Wrote one myself :) – cmhughes Sep 13 '17 at 16:19

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.