Pretty printing of LaTeX code

Under normal circumstances (i.e. not in verbatim or similar), TeX converts line ends to spaces and combines multiple spaces into a single space. It also skips spaces at the start of a line. Thus

% Comment to show start of text
\usepackage{foo}

and

% Comment to show start of text
   \usepackage{foo}% Note spaces

are equivalent. In the same way

\hypersetup{%      % Konfiguration hyperref
  pdftitle={Titel} % Titel
 ,pdfauthor={username} % Verfasser

is equivalent to

\hypersetup{pdftitle={Titel} ,pdfauthor={username} ...

(note the space before the comma). Most LaTeX keyval implementations ignore spaces 'before' and 'after' each entry, so

\setkeys{somepkg}{foo=bar,foo2=bar2}

and

\setkeys{somepkg}{  foo=bar  ,  foo2=bar2  }

are equivalent, meaning that the space in the hyperref line is also fine. It's worth noting that most LaTeX keyval implementations also ignore spaces around the =, so that

foo = bar

and

foo=bar

are equivalent. (This is not true for ConTeXt. It's also worth noting that datatool uses a keyval implementation which is much less forgiving on spaces.)

Not directly related, but many people prefer having the commas at the end of the line

\hypersetup{%      % Konfiguration hyperref
  pdftitle={Titel}, % Titel
  pdfauthor={username}, % Verfasser
% linktoc=all      % Alles als Link setzen
}  

which follows exactly the same rules: you can have the commas 'flush' or aligned and spaces will still be ignored.


I created a website that reformats the latex code to make indents correct. Note the nice thing about this system it ignores the initial header and keeps formatting of \usepackage{}.

The website focus is mainly on helping organize the latex so you can tell where indentation of new blocks of equations or lists are. The focus is not on adding comments, or making empty blocks so comments can be inserted. The focus is mainly

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

Tags:

Sourcecode