In many MWE here on TeX.SE the LaTeX code is written without explanatory comments and as compact as possible. An Example:

Blindtext. \url{http://tex.stackexchange.com}.

This is particularly difficult to read for LaTeX beginners and to understand. So I got used a certain style of writing MWEs. I use a space (to be changed to a % character if neccessary) in front of macros like \usepackage{} or I write each option for a document class or a package in a seperate line with a leading coma. So I'm able to comment the meaning of the option. And one can see which lines are commented. Added now to the example: You can easy switch between two fonts or two encriptions. The same example pretty printed:

%,paper=a4           % Papiergröße (Voreinstellung)
 ,fontsize=12pt      % Brotschrift-Grad
%,parskip=full       % Abstand Absätze, erste Zeile nicht eingezogen.
 ,pagesize           % entscheidet zur Laufzeit, ob dvips oder pdf

 \usepackage{lmodern}          % Latin Modern
%\usepackage{libertine}        % Libertine Legacy
 \usepackage[T1]{fontenc}      % Ausgabezeichensatz
 \usepackage[latin9]{inputenc} % Windows PC
%\usepackage[utf8]{inputenc}   % Unicode
   backref=page                % Verweis auf Seite
% ,pagebackref                 % wie zuvor
  ,breaklinks                  % Links überleben einen Zeilenumbruch 
  ,colorlinks                  % Links farbig für PDF-Betrachtung 

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

Blindtext. \url{http://tex.stackexchange.com}.

Question: Are there any objections to format a LaTeX code like this? For example,

  • can the leading space of macro \usepackage{} lead to errors,

  • can the notation ,option = value lead to errors (I prefer this kind of notation to get rid of missing commas. If all commas are in the same column, it is much harder to forget one (the first line must not have a leading comma)).

  • Wouldn't commented-out lines be rather meaningless in a minimal working example? There'd be one or two, at most. – You Jan 10 '13 at 9:40
  • 2
    For proper indentation of your LaTeX code, you could consider using latexindent. – Werner Jan 28 '13 at 19:44

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


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

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



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.

| improve this answer | |

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


| improve this answer | |
  • I'm not sure what that Web site is supposed to do: I pasted the first code example in the question and clicked on “Submit”. The result is the same identical code. Then I removed some end-of-lines, resubmitted and the result was the same code (even without new lines). – egreg Oct 5 '15 at 7:59
  • It indents your code. It does not remove new lines. It just makes sure that indentation is correct. – Whitecat Oct 5 '15 at 14:09

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.