Help:Editing/House Style/LaTeX Code Style

From ProofWiki
Jump to navigation Jump to search

$\LaTeX$ Code Style

There are a few general source code conventions which make your code easier to read and maintain:

  • Each variable, and each command beginning with a backslash should be preceded by a space, except (for some unexplained result of evolution) when enclosing things in brackets. See some of the above instance for a typical example.
  • When enclosing brackets around an object, always use the \paren { ... } command, for example \paren {a + b} rather than (a + b).
  • There should be no need to use the commands \big, \Big, \Bigg etc. for specifying the sizes of parentheses. Using the \paren {...} technique (as above) will almost always automatically size the brackets aesthetically.
  • Punctuation should appear (if it is really necessary) outside the $\LaTeX$ environment, for example:
Hence $\map f {\sin x}$. (produced by: $\map f {\sin x}$.)
as opposed to:
Hence $\map f {\sin x}.$ (produced by: $\map f {\sin x}.$)
  • Single-character parameters to standard $\LaTeX$ constructs need not be put in curly braces. That is, \dfrac 1 2 is preferred to \dfrac {1} {2}. They both produce: $\dfrac 1 2$

It makes the source code significantly easier to read.

Having said that, please do not ignore the rule about spacing. The same effect can also be achieved by \dfrac12 (see, it still looks like $\dfrac12$) but that is significantly harder to interpret visually.


Aligned Material

If an equation includes multiple equalities or inequalities, it is best to place each equality on a new line.

For example:

$\dfrac \d {\d t} \map H U = \d \map H U \cdot \dot U = \map \Omega {\map {X_H} U, \dot U} = \map \Omega {\map {X_H} U, \map {X_H} U} = 0$

would look better as an aligned equation. This is done using the following commands:

{{begin-eqn}}
{{eqn | l = 
      | r =
}}
  ...
{{eqn | l = 
      | r =
}}
{{end-eqn}}

Here, the section following | l = is a $\LaTeX$ environment, and should contain anything you want to appear to the left of the equals sign.

The section following | r = is the same, but will appear to the right of the equals sign.

| ll = and | rr = are similar, but produce material in columns further to the left and further to the right respectively. In particular, the ll column is often used for an "implies" or "leads to" sign where the l and r are used for either side of a string of equations.

All these $\LaTeX$ environments are already in \displaystyle mode, so there is no need to include that command in your equation.

When entering such an {{eqn}} environment, it should globally look like this:

{{eqn | l = 1 + 1
      | r = 2
}}

That is, it adheres to the following principles:

  • Every empty column should in general be omitted, except perhaps for | c = sections, which can be left as placeholders for possible future addition of comments
  • Non-empty columns are entered on separate lines, with the | and = all aligned.

These conventions serve to optimize readability.

More options and abilities of the {{eqn}} can be found on its page, {{eqn}}.


The section following | c = is not a $\LaTeX$ environment, and can be used to add any comments about the equation at this point.


So the example we gave above would be typeset as:

{{begin-eqn}}
{{eqn | l = \frac \d {\d t} \map H U
      | r = \d \map H U \cdot \dot U
      | c = [[Chain Rule for Derivatives]]
}}
{{eqn | r = \map \Omega {\map {X_H} U, \dot U}
      | c = Definition of $X_H$
}}
{{eqn | r = \map \Omega {\map {X_H} U, \map {X_H} U}
      | c = [[Hamilton's Equations]]
}}
{{eqn | r = 0
      | c = from above: $\Omega$ is [[Definition:Skew-Symmetry|skew-symmetric]]
}}
{{end-eqn}}

The finished result will look like:

\(\ds \frac \d {\d t} \map H U\) \(=\) \(\ds \d \map H U \cdot \dot U\) Chain Rule for Derivatives
\(\ds \) \(=\) \(\ds \map \Omega {\map {X_H} U, \dot U}\) Definition of $X_H$
\(\ds \) \(=\) \(\ds \map \Omega {\map {X_H} U, \map {X_H} U}\) Hamilton's Equations
\(\ds \) \(=\) \(\ds 0\) from above: $\Omega$ is skew-symmetric

The operator that is displayed in this template can be changed using | o = to show inequalities, etc.

Note the following:

  • Do not include two consecutive open or close curly braces: {{ or }} anywhere in your {{eqn}} templates. It will break the interpreter.

Put spaces in: { { or } } and it will be okay.

  • Do not include the vertical line | (a.k.a. "pipe") in $\LaTeX$ expressions as this also breaks the interpreter. Use \vert (or \lvert and \rvert) instead.
  • In particular, \| (used to produce $\|$) has the same problem. Use \Vert etc. instead.

These caveats apply only within the {{eqn}} environment. Elsewhere on the page such constructs should be fine. To accommodate for the inevitable copy-paste efforts, and for consistency's sake, it is however desirable to always use \vert and \Vert, and to insert a space between adjacent curly braces within $\LaTeX$ strings.