Latex - Stuff for Advanced Writers

From Torben's Wiki


Advanced newcommand for variable+number+unit

Defining commands for variables might be usefull to ensure italics when used in text, or to save some typing for vaviables with subscripts

\usepackage{xspace} 
...
\newcommand{\Ea} [0] {\ensuremath{E_\text{act}\xspace}
% \xspace ensures space after parameterless command, if not followed by punctuation
The \Ea is measured to $\Ea=123\,\text{eV}$ 

More advanced we can define a command for a variable that also prints a value and unit if a parameter is given

\usepackage[noload={abbr}]{siunitx}
\usepackage{xspace}
\usepackage{ifthen}
...
\newcommand{\Ea} [1][\empty]
{%
\ifthenelse%
{\equal{#1}{\empty}}%
{\ensuremath{E_\text{act}}\xspace}%
{\mbox{\ensuremath{E_\text{act}=\SI{#1}{\electronvolt}}}%
}
The \Ea is measured to \Ea[123]

Create your own personal Bibtex style

Just run

latex makebst

(in Linux the package texlive-bibtex-extra is required for this)
and answer quite a lot of questions (help can be found here [1] and [2]). This produces a batch (.dbj) file, that can be converted into a bibtex style if you say so at the last question, or by manually running latex FILENAME you chose)

If you want to re-run it with only little changes, you might prefer to edit the batchfile instead...
Alternative: Copy the Bibtex style of a famous journal. Here is a long list

Display textwidth and columnwidth in cm

Important for generating images at the maximum size without scaling

\usepackage{layouts}
...
\printinunitsof{cm}\prntlen{\textwidth} %   = 17.9219
\printinunitsof{cm}\prntlen{\columnwidth} % =  8.64468


Calculate Length

e.g. to calculate the width needed to fit an image [3]

\newdimen\boxbreite
\advance\boxbreite by 0.3cm
\advance\boxbreite by 10cm
%Ergebnis: \the\boxbreite.

compare Units of length used in Latex

Footnote inside caption of figure/table

\stepcounter{footnote}           % Fußnotenzähler weitersetzen          
\footnotetext{Text der Fußnote}          
\begin{figure}            
 \includegraphics{bild.eps}      % Hochstellung geht nur im Mathemodus ($...$)             
                                 % damit die richtige Zahl genommen wird, den footnote-Zähler verwenden             
\caption{Bildbeschriftung.${}^{\thefootnote}$}  

Source: DanteFAQ

PDF Warnings

"Token not allowed in a PDF string"
Solution:

\texorpdfstring{Code im Text}{Code für bookmark}
\texorpdfstring{C$_{60}$}{C60}

Rotate Text

\rotatebox{90}{Text gedreht}

Reuse old equation number

I want set the equation number of a single equation by hand. (used when an equation that had already been defined, is used later on again. Solution via the \tag command:

\begin{align}
a \label{eq:asdf}\\
b \\
a \tag{\ref{eq:asdf}} \\
c
\end{align}

For some reason using the \tag command insite an "equation" instead of an "align" sometime results in the following error:

destination with the same identifier has been already used, duplicate ignored

Bold Math in Section Title

\section{C$_{60}$} % looks ugly (non-bold "60")
\section{\boldmath C$_{60}$} % looks right, but bold also in TOC :-(
\section[C$_{60}$]{\boldmath C$_{60}$} % looks nice also in text and TOC, but produces a warning
\section[\texorpdfstring{C$_{60}$}{C60}]{\boldmath C$_{60}$} % = SOLUTION

equation, eqnarray or align?

Just use align! (see this example or l2tabu.pdf)

\begin{align}
  z_0 &= d = 0 \\
  z_{n+1} &= z_n^2+c
\end{align}

Float Barrier

\usepackage{placeins}
% defines the command \FloatBarrier: the name is program, no from above are allowed to pass this barrier
% [section] : floats are not allowed to travel into another section

Nicer Tables

In tables it is nice if numbers are aligned at the decimal point:

  1.234
100.3
 23.23

The easiest way to achieve this is using invisible \phantom chars:

\phantom{00}1.234
          100.3\phantom{00}
\phantom{0}23.23\phantom{0}


Rotated Table (Tabelle im Querformat)

\usepackage{longtable}
\usepackage{rotating}
...
\begin{sidewaystable}
  \centering
  \begin{longtable}{|lll|}
    ...
    table body
    ...
    \caption{caption}
    \label{tab:label}
  \end{longtable}
\end{sidewaystable}

Rotate single cells of a table

\usepackage{rotating}
...
\begin{tabular}{|c|c|}\hline
Name &\begin{sideways}Grade\end{sideways} \\
\hline
Name 1 & 3 \\
Name 2 & 1 \\
\hline
\end{tabular}

Bibtex Problems

Short Authors Keys + Umlaute

When using a bib style that uses keys like "Jon90", there might be problems with author names including umlautes (äöüß). In the bibfile you have to make sure, that in the author fields the names are written with latex formated umlauts:

Kröger -> Kr{\"{o}}ger

Tracking Changes

latexdiff can be used to track changes done to a document, generates a nice colored output

latexdiff old.tex new.tex > diff.tex
pdflatex diff.tex
bibtex diff
pdflatex diff.tex ; pdflatex diff.tex

from Christian K

Verteilung von Bildern auf einer Seite

\renewcommand{\topfraction}{.8} % max 80% darf von oben mit Bildern belegt sein
\renewcommand{\bottomfraction}{.8} % dasselbe von unten
\renewcommand{\floatpagefraction}{.7} %  max. 70% der ganzen seite darf mit Bildern belegt sein
\renewcommand{\textfraction}{.2} % soviel muss text sein
\setcounter{bottomnumber}{1} % verhindert dass zwei floats aneinandergepappt werden, wenn man das nicht will
\setcounter{topnumber}{1} 

Wenn eine lange Fußzeile mal auf mehrere Seiten verteilt wird, hilft \interfootnotelinepenalty=10000

So kann man die Zeilenhöhe von Tabellen etwas größer machen: \renewcommand{\arraystretch}{1.4}

Fußnoten in Tabellen kann man mit dem Paket threeparttable realisieren.