Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

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

\documentclass[ngerman,fontsize=12pt,pagesize]{scrreprt}
\usepackage{lmodern} 
\usepackage[T1]{fontenc} 
\usepackage[latin9]{inputenc} 
\usepackage[backref=page,breaklinks,colorlinks]{hyperref}
\hypersetup{pdftitle={Titel},pdfauthor={Kurt}}
\begin{document}
Blindtext. \url{http://tex.stackexchange.com}.
\end{document}

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:

\documentclass[%
  ngerman
%,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
]{scrreprt}

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

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

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

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

share|improve this question
    
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
    
To show possible alternatives, why not? Or to make testing easier? Minimal first mean in my opinion that it is complete, compilable (sometimes only with the error), and calls only packages useful to rebuild the error. –  Kurt Jan 10 '13 at 23:30
2  
For proper indentation of your LaTeX code, you could consider using latexindent. –  Werner Jan 28 '13 at 19:44

1 Answer 1

up vote 20 down vote accepted

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={Kurt} % Verfasser

is equivalent to

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

(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={Kurt}, % 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.

share|improve this answer
1  
A leading comma I prefer because it is an additional visual marker of a lower level in the code (in Pascal I used the semicolon in this way to get pretty printed programs). –  Kurt Aug 18 '12 at 0:20
    
@Kurt I'm not quite clear on 'lower level' here, but it really does not matter as this is about author style and not the result (hopefully my answer makes that clear). –  Joseph Wright Aug 18 '12 at 9:00
    
Your answer was clear. I interpret as first level the start of a macro, the second level (with leading blank and comma) are the options, if an option has a list of values like name=preset{name1=value1,name2=value} I would write with more leading blanks {% (new line name1=value1 newline ,name2=value2 newline }. ,name2=value2 would be start at level 2. Could I make me clear? –  Kurt Aug 18 '12 at 9:40

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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