LaTeX

From TechWiki

Installation see: here.

Contents

In a Nutshell

Compile

latex name.tex

Up to three times for all cross-references to be right.

Compile with bibliography

latex name.tex
bibtex name
latex name.tex
latex name.tex

Editor

xemacs name.tex &

or

kate name.tex &

DVI Viewer

xdvi name.dvi &

Postscript (PS)

Convert from DVI to PS:

dvips -o name.ps name.dvi

and view with Ghostview (GV)

gv name.ps &

PDF

From dvi File
dvips name.dvi
From ps File

Use:

ps4pdf name.tex

If you want to include eps graphics and pdf's, use

 \PSforPDF{\includegraphics[...]{myFig.eps}}

Old:

Instead of compiling with the latex command and converting to PS with dvips and the using

ps2pdf name.ps

you can simply compile with

pdflatex name.tex

However, all figures must also be in PDF format, so you'll need to convert them from EPS (encapsulated PS)

epstopdf fig.eps

Problems: psfrag won't be able to replace text in the figures with LaTeX rendered text.

Graphics

Use \usepackage{graphicx} to include package.

\begin{figure}[!t]
       \begin{center}
                \includegraphics[scale=0.45]{fig/dc.eps}
                \includegraphics[scale=0.45]{fig/lb_vs_os.eps}
                \caption[Name for TOC]
                {\label{fig} Blabla...}
        \end{center}
\end{figure}

You can also rotate diagrams:

\includegraphics[angle=-90,scale=0.25]{fig.eps}

psfrag

Handy tool to replace existing text in figures with LaTeX rendered text. Include package with \usepackage{psfrag}.

\psfrag{tag_in_fig}{replacement}
\psfrag{<tag>}[latex ref point: <t,b,B>,<l,r>][ps ref point (like latex ref pt)][scale][rot]{repl}

Example:

\psfrag{YLAB}[][][0.75]{Market Value (\%)}

Hint: if you can't place the labels right, you can try with \phantom{}

\psfrag{YLAB}[r][r][0.75]{Market Value (\%) \phantom{xxxxxxxxxxxx}}

Symbols

Use \mathbb{R} to get the symbol for the set of real numbers.

Use \textrm if \text doesn't work.

In the AMS package, \mathcal{L} gives nice symbols.

Equations

Multirow Equations

If you need to break an equation with \left( ... \right) or similar in it, you need to "close" each bracket by hand using

\left( ... \right.
\left. ... \right)
eqnarray

With

\begin{eqnarray}
  a & = b \\
  c & = c
\end{eqnarray}

you get two numbered equations that are aligned where the '&' symbol is set. If you only want one number, use \nonumber.

align

If you want one number for a multirow equation nicely centered, use \begin{eqnarray}

\begin{align}
  \begin{split}
    a & = b \\
    c & = c
  \end{split}
\end{align}

If you want numbering like "(9a)", use the subequations environment, i.e.,

 \begin{subequations}
  \begin{align}
    ...

Bibtex

A great tool for organizing and manipulating big .bib files is jabref.

Create bibliography text file, e.g., /home/user/bibtex/books.bib:

 @BOOK{LOTC,
       AUTHOR = {Lee Smolin},
       EDITOR = {},
       TITLE = {The Life of the Cosmos},
       PUBLISHER = {Oxford University Press},
       YEAR ="1997",
       MONTH ="",
       VOLUME = "",
       SERIES = "",
       EDITION = "",
       ADDRESS = {},
       ID = NKS
   }

Get a bibtex style file, e.g., my_bibtex_style.bst.

Set environment variables: Bash:

export BIBINPUTS=".:/home/user/bibtex/"
export BSTINPUTS=".:/home/user/bst/"

Csh:

BIBINPUTS=.:/home/user/bibtex/
BSTINPUTS=.:/home/user/bst/

Adding bibliography to a .tex file (at the end of the document):

\pagebreak

\bibliography{books}
\bibliographystyle{your_bst_file}

\end{document}

The my_bibtex_style.bst mentioned above, gives the following result:

If you use

\bibliographystyle{plain}

then the references are cited with numbers, e.g., [1].

Natbib Style

For author-year citations, use package

\usepackage[compress,authoryear]{natbib}

and

\bibliographystyle{plainnat}

and optionally

citep{}

instead of cite{}

For numbers, use package

\usepackage[sort&compress,numbers]{natbib} 

and

\bibliographystyle{plainnat}

GUI Tool

JabRef:

http://jabref.sourceforge.net/

Including Figures

Figures are placed as floats by LaTeX somewhere in the document. Using

\begin{figure}[h!]

you can force LaTeX to place the figure where the figure environment is defined in the text.

If you have too many figures, you can avoid trouble by placing a

\clearpage

at the end of each page.

Version 1.1

\begin{figure}[]
        \begin{center}
                \includegraphics[scale=0.41]{figures/tmx1_noem.epsi}
                \includegraphics[scale=0.41]{figures/tmx1_em.epsi}
                \caption[Text for TOC] {Text}
        \end{center}
\end{figure}

Version 1.2

\begin{figure}[]
    \centering
    \begin{tabular}{ll}
    \begin{minipage}{0.0in}
    \centering
    \includegraphics[scale=0.5]{f1.pdf}
    \end{minipage}
    \begin{minipage}{9.4in}
    \centering
    \includegraphics[scale=0.3]{f2.pdf}
    \end{minipage}
    \end{tabular}
    \caption{...} 
    \end{figure}

Version 2.1

\begin{figure}[]
        \begin{minipage}[b]{0.5\textwidth}
                \centering
                \includegraphics[angle=-90,width=0.975\textwidth]{fig/f1.epsi}
                \includegraphics[angle=-90,width=0.975\textwidth]{fig/f2epsi}
        \end{minipage}%
        \begin{minipage}[b]{0.5\textwidth}
                \centering
                \includegraphics[angle=-90,width=0.975\textwidth]{fig/f3.epsi}
                \includegraphics[angle=-90,width=0.975\textwidth]{fig/f4.epsi}
        \end{minipage}
        \caption[Text for TOC] {Text}
\end{figure}

Version 2.2

\begin{figure}[]
        \begin{minipage}[b]{0.5\textwidth}
                \centering
                \includegraphics[angle=-90,width=0.975\textwidth]{fig/f1.epsi}
                \vfill{\phantom{.}}
                \includegraphics[angle=-90,width=0.975\textwidth]{fig/f2epsi}
        \end{minipage}%
        \begin{minipage}[b]{0.5\textwidth}
                \centering
                \includegraphics[angle=-90,width=0.975\textwidth]{fig/f3.epsi}
                 \vfill{\phantom{.}}
                \includegraphics[angle=-90,width=0.975\textwidth]{fig/f4.epsi}
        \end{minipage}
        \caption[Text for TOC] {Text}
\end{figure}

Version 3

Using the package subfigure

\begin{figure}[ht]
\centering
     \subfigure[Cap1]{
          \label{fig:1}
          \includegraphics[scale=0.3,angle=-90]{fig1.epsi\}}
     \hspace{.3in}
     \subfigure[Cap2]{
          \label{fig:2}
          \includegraphics[scale=0.3,angle=-90]{fig2.eps}}\\
     \subfigure[Cap3]{
           \label{fig:3}
           \includegraphics[scale=0.3,angle=-90]{fig3.eps}}
     \hspace{.3in}
     \subfigure[Cap4]{
          \label{fig:4}
          \includegraphics[scale=0.3,angle=-90]{fig4.eps}}
     \caption{Total cap.}
     \label{fig:tot}
\end{figure}

But any \psfrag{} commands either before the \includegraphics{} for individual formatting, or at the beginning if they apply for all subplots.

Tables

LaTeX table with customized line spacing:

\begin{table}
\caption{Table caption.}
\label{t.lbl}
\centering
\textsf{
\begin{tabular}{lcrccc} \\[-4.2ex]
\hline  \\[-5.5ex]
Currency & E & $\Delta$ E & C  & $\Delta$ C\\  [-0.5ex]
\hline \\[-5.5ex]
AUD-JPY & 2.1 & $\pm$ 0.2  & 2.1 & $\pm$ 0.2\\ [-2.2ex]
AUD-JPY & 2.1 & $\pm$ 0.2  & 2.1 & $\pm$ 0.2\\ [-2.2ex]
AUD-JPY & 2.1 & $\pm$ 0.2  & 2.1 & $\pm$ 0.2\\  [-0.5ex]
\hline
\end{tabular}
}
\end{table}

Customizing

Numbering of Sections etc.

Roman numbering

\renewcommand{\thesection}{\Roman{section}}
\renewcommand{\thechapter}{\Roman{chapter}}


Installing Packages

Know what your LaTeX path ($TEXMF) is. On kubuntu it's

TEXMF=/usr/share/texmf-texlive/

Update the database

sudo texconfig rehash

and update the format file

sudo fmtutil --refresh
mathabx

See techlogbook.wordpress.com

Page Format

To change the page width and height, put

\setlength\topmargin{0in}
\setlength\headheight{0in}
\setlength\textheight{22cm}
\setlength\textwidth{15.5cm}
\setlength\oddsidemargin{0in}
\setlength\evensidemargin{0in}

after the package declaration.

References

To add the title References in your table of content (\tableofcontents) put this line of code

\addcontentsline{toc}{section}{References}

above

\bibliography{books,journals,...}
\bibliographystyle{mystile_bst}

To use multiple bibliographies, see www.tex.ac.uk.

Table Row and Column Spacing

Alter distance between rows

\renewcommand{\arraystretch}{x}

Alter distance between columns

\setlength{\tabcolsep}{10pt}

Figure and Table Numbering

The default numbering scheme is "Figure 1" etc. If you would like to customize this, e.g., have "Figure SI-1", use

\renewcommand{\thefigure}{SI-\arabic{figure}}
\renewcommand{\thetable}{SI-\arabic{table}}

To just change the naming, e.g., "FIG." instead of"Figure", use

\renewcommand{\figurename}{FIG.}
\renewcommand{\tablename}{TBL.}

To modify the numbering

\addtocounter{table}{-1}
\refstepcounter{table}

For more customization see ctan.tug.org. E.g., to change the colon to a period after the figure name, set the name in bold, and the text with sanserif font, use

\usepackage[labelfont=bf,textfont=sf,labelsep=period]{caption}

Comments

Multi line commenting (from here). In preamble

\newcommand{\comment}[1]{} 

and then

\comment{
...
}

Footnotes

For multiple references to one footnote, label the footnote

...text\footnote{Blabla. \label{foot}} more text...

and then reference it again as

...my second reference\textsuperscript{\ref{foot}} to the previous footnote...

Appendix

\appendix
\renewcommand\thesection{Appendix \Alph{section}}

puts "Appendix" in the section names: "Appendix A Bla Bla". From /www.cse.iitd.ernet.in.

Letters

\documentclass{letter}

%\usepackage[a4paper]{geometry}
\usepackage[top=2cm, bottom=2cm, left=3.5cm, right=3.5cm]{geometry} 

% Info
\address{       Me\\
                ...}
\date{\today}
\signature{Me}

% Document
\begin{document}

\begin{letter}{Their address...}

\opening{Dear Editor,}

Text...

\closing{Yours faithfully,}

\end{letter}
\end{document}

Slides

For creating LaTeX presentations, the beamer class is pretty cool.

Documentation:

Install

apt-get install latex-beamer

will install the packages in the TEXMF directory (e.g., /usr/share/texmf/tex/latex/).

You can do this by hand if you get the code from https://sourceforge.net/projects/latex-beamer/.

Compile

Compile with

  • pdflatex name.tex

or

  • ps4pdf name.tex

Note that you will need to include .pdf figures or use

\PSforPDF{\includegraphics[...]{myFig.eps}}

for .eps (encapsulated postscript).

Example

See the PDF: example.pdf

\documentclass{beamer}

 \usepackage{epsfig} 

\mode<presentation>
{
  \usetheme{JuanLesPins}
 \usecolortheme{beaver}
}

\title{This is the Title}
\author{Me}
\date{\today}
\logo{\epsfig{file=logo.pdf,height=20ex}}

\begin{document}

\frame{\titlepage}

\section[Outline]{}
\frame{\tableofcontents}

\section{Introduction}

\frame
{
  \frametitle{Frame Title}

  \begin{itemize}
  \item<1-> Item 1.
\bigskip
  \item<2-> Item 2.
\bigskip
  \item<3-> Item 3.
  \end{itemize}
}

\section{One}

\frame
{
  \frametitle{Another Frame}

   \begin{block}{Titel of Block}
    Text \\ More text ...
  \end{block}
\bigskip
\pause
  \begin{block}{Titel of $2^{\text{nd}}$ Block}
    Text \\ More text ...
  \end{block}
\medskip
  Some more text\footnote{with footnote.}...
}

\section{Two}

\subsection{First}

\frame
{
  \frametitle{Features}

And of course the nice \LaTeX  \ equation rendering:
\begin{equation}
\mathcal{L} = \bar \psi (i \hbar c \not\!\partial - mc^2) \psi.
\end{equation}
\pause
\begin{equation}
\bar \psi = \psi^\dagger \gamma^0; \ \not\!\partial = \gamma^\sigma \partial_\sigma
\end{equation}
}

\subsection{Second}

\frame
{
  \frametitle{Figure}

\begin{figure}
\includegraphics[scale=0.5]{fig.pdf} 
\caption{An example of a figure}
\end{figure}

}

\subsection{Third}

\frame
{
\frametitle{Two Figures}

\begin{figure}
\centering
\begin{tabular}{cc}
\begin{minipage}{1.75in}
\centering
\includegraphics[scale=0.35]{fig.pdf}
\end{minipage}
\begin{minipage}{1.5in}
\centering
\includegraphics[scale=0.35]{fig.pdf}
\end{minipage}
\end{tabular}
\caption{Scaling-law probability density function} 
\end{figure}
}

\end{document}

Using colums:

\begin{columns}
    \begin{column}{0.5\textwidth}
        \begin{tabular}{ | l |  r | r |}
                \hline
                & $\hat h$ & $HI$ \\
                \hline
                 US & 5.91 & 0.7351 \\
                 GB & 5.89 & 0.4245 \\
                \hline
        \end{tabular}
    \end{column}
    \begin{column}{0.5\textwidth}
        \begin{figure}[t]
           \PSforPDF{\includegraphics[width=1.0\textwidth]{someFig.eps}}
            \caption{Just some text...} 
        \end{figure}
    \end{column}
\end{columns}

Handouts

To create handouts with 4 slides on one page, run

pdfnup --nup 2x2 --orient landscape --frame true --delta \""3mm  3mm\"" 
       --column true --columnstrict true --noautoscale false --scale 0.95 mySlides.pdf

And HTML

This is still a problem.

Although there are plenty of tools (German), nothing is really that great.

latex2html

This is perhaps the widest used converter and it can deal with complex LaTeX stuff. However, I always get ugly rendered equations (black borders and messed up alignement). Support has stopped in 2001.

Links:

MediaWiki

Perhaps a modern solution would be to use MediaWikis, the Wikipedia wiki, support of LaTeX. See LaTeX for examples of using LaTeX with MediaWiki.

There are two options. Firstly, the "light" version uses Texvc (handbook), which gives you LaTeX support. See here for installation manual.

WikiTex

Secondly, you could go for the Wikipedia: WikiTeX solution, an expansible LaTeX module for MediaWiki. It not only gives you math, but chess, chemistry, Feynman, go, graphs, music, plotting, SVG and even Tengwar, a script languange created by J. R. R. Tolkien.

The project called wikisophia.org, houses WikiTeX, an expansible LaTeX module for MediaWiki.

MathML

Another solution is to use MathML (see also Mozilla's MathML page).

I find it a bit tedious, as it uses the XML syntax style employing tags. So you need this code

 <math>
  <mi>x</mi>
  <mo>=</mo>
  <mfrac>
    <mrow>
      <mrow>
        <mo>-</mo>
        <mi>b</mi>
      </mrow>
      <mo>&PlusMinus;</mo>
      <msqrt>
        <msup>
          <mi>b</mi>
          <mn>2</mn>
        </msup>
        <mo>-</mo>
        <mrow>
          <mn>4</mn>
          <mo>&InvisibleTimes;</mo>
          <mi>a</mi>
          <mo>&InvisibleTimes;</mo>
          <mi>c</mi>
        </mrow>
      </msqrt>
    </mrow>
    <mrow>
      <mn>2</mn>
      <mo>&InvisibleTimes;</mo>
      <mi>a</mi>
    </mrow>
  </mfrac>
 </math>

to get this result

x = \frac{-b \pm \sqrt{b^2 - 4ac}}{2a}

which in LaTeX is simply

x = \frac{-b \pm \sqrt{b^2 - 4ac}}{2a}

However, there are LaTeX to MathML converters: tex4moz and itex2mml.

Unfortunately 'legacy' browsers such as IE5.x and Netscape 4.x are not able to render MathML directly...


PSTricks

See under PSTricks.

References

Problems

Long URLs and overfull/underfull Lines

If you cite URLs in the bibliography, they can be large and mess up LaTeX's line spacing.

The line that produces

http://www.arXiv.org/abs/cond-mat/9910332

in my bst style file is

{"\textsf{http://www.arXiv.org/abs/" eprint * "}" *}

This should be modified to contain

\linebreak[0]

i.e.,

{"\textsf{http://\linebreak[0]www.arXiv.org/abs/" eprint * "}" *}

which allows LaTeX to optionally break the current line at that point, fixing the formatting problem.

Actually, there is an url package (\usepackage{url}) which deals with line breaks...

Graphics Vertical Alignement

Using graphicx package allows you to put multiple diagrams n a figure. However, sometimes the vertical alignment is messed up. As an example, if you have a figure consisting of 2x2 plots, i.e.,

\includegraphics[scale=0.45]{fig1.eps}
\includegraphics[scale=0.45]{fig2.eps}
\includegraphics[scale=0.45]{fig3.eps}
\includegraphics[scale=0.45]{fig4.eps}

it can happen that the top two plots are too close to the bottom two.

The only solution I found is a bit of a hack:

\begin{figure}[!t]
        \begin{minipage}[t]{.5\textwidth}
                \centering
                \includegraphics[scale=0.25]{fig1.eps}
                 \vfill{\phantom{.}}
                \includegraphics[scale=0.25]{fig2.eps}
        \end{minipage}%
        \begin{minipage}[t]{.5\textwidth}
                \centering
                \includegraphics[scale=0.25]{fig3.eps}
                \vfill{\phantom{.}}
                \includegraphics[scale=0.25]{fig4.eps}
        \end{minipage}
        \caption[Name for TOC] {\label{fig}....}
\end{figure}

First you need to use

\begin{minipage}[t]{.5\textwidth}

twice, here the width is given relative to \textwidth. The trick is to use

\vfill

in every minipage, where

 phantom{.}

creates an invisible dot, which forces the vertical alignment to increase. Using \vfill{\phantom{A}} would create more vertical space.

For the optimal width of the diagrams you can also use

\includegraphics[width=0.95\textwidth]{fig4.eps}

which ensures that each element will fill 95% of each minipage.

Note the "%" sign after the first minipage block. This required otherwise the compiler complains about Overfull \hbox and stuff...

Verbatim Footnotes

Couldn't get verbatim (\verb or \begin{verbatim}) footnote text. Used \usepackage{url} instead:

\footnote{\url{www.url.net}.}

Memory Limit

If you encounter this

TeX capacity exceeded, sorry [main memory size=1000000].

then make the memory larger.

Read:

less /etc/texmf/texmf.cnf

Edit:

vim /etc/texmf/texmf.d/XXX.cnf

For memory you are looking for main_memory in 95NonPath.cnf.

Update LaTeX:

sudo update-texmf
sudo fmtutil --all 

Line Height in Tables

Doing

\begin{table}
\caption{Table caption.}
\label{t.lbl}
\begin{center}
\begin{tabular}[ht]{|l|c|c|c|}
  \hline 
    & $\hat h_n$ & $\overline S$ & $\overline H$ \\
  \hline\hline
  ...

makes the hat on h touch the top line. After unsuccesfully messing around with

\\ [2ex]

and such, this hack works: add a phantom expression that is larger:

& \phantom{$\hat \frac{1}{2}$} $\hat h_n$ & ..

See also Tables.

Including PSTricks Figures

Sometimes PSTricks figures can't be correctly restricted by a bounding box. I.e.,

latex pst.tex
dvips -o pst.ps pst.dvi
ps2eps pst.ps

fails.

A workaround is

latex pst.tex
dvips -o pst.ps pst.dvi
ps2pdf pst.ps
pdf2ps pst.pdf

For this to work, you need

\usepackage{pst-pdf}

and the correct delimitations

\begin{pspicture}(x1,y1)(x2,y2)

Unfortunately this sometimes leads to figures which can't be centered, see next problem...

Centering Figures

For some odd reason (see also last problem description), using PSTricks, its sometimes hard to generate a PS figure that can be centered in a two-column document. A first try is using the option H, i.e.,

 \begin{figure}[H]

(which may require the float package). Although this works, it messes up the correct placement of paragraphs, as it simply interrupts the text flow and places the figure where it appears in the document. Using

 \begin{figure}[tH]

messes with the centering again...

Workaround: Use left centering

\begin{figure}[h]
 \begin{flushleft}
  \includegraphicss[width=0.395\textwidth]{...}
 \end{flushleft}
\end{figure}

Now adjust the width parameter, so the figure is as wide as the column.

ps2eps

Sometimes ps2eps doesn't work to correctly set the bounding bow. Use

latex file.tex
dvips -o file.ps file.dvi
ps2eps file.ps
ps2epsi file.eps

and use file.epsi.

Alternatively, this sometimes works

latex file.tex
dvips -o file.ps file.dvi
ps2pdf file.ps
pdf2ps file.pdf

and use file.ps.

pdf2ps

Sometimes pdf2ps creates ridiculously huge ps-files when the pdf was generated using

pdflatex myFile.tex

Alternatively, this works better on the same pdf

pdftops myFile.pdf
ps2eps myFile.ps

Compression Artifacts from ps2pdf

Sometimes ps2pdf messes up figures by adding ugly compression artifacts to them, i.e., random noise around coutures and such. Check that your .ps is fine.

To get lossless compression use

ps2pdf -dAutoFilterColorImages=false -dColorImageFilter=/FlateEncode myFile.ps

From cosmocoffee.info.

Bounding Box

Using processing.org to export a tiff which in turn is modified using inkscape (creating a svg) can have a problem if you want use the resulting figure in a LaTeX document: what looks like a correct bounding box for the ps or eps figures is lost in the dvi or pdf of the LaTeX document.

Either:

  • save the svg in inkscape as pdf (select the "via Cairo" option) and do pdf2ps

or

  • save the svg as
    • (via Cairo) to eps
    • (via Cairo) to ps

But now you may have compression artifacts, see above.

Miscellaneous

To do a word count of a LaTeX document, use detex as seen in Linux_Commands.

Resources

www.cse.iitd.ernet.in