LaTeX/Tips and Tricks

From Wikibooks, open books for an open world
Jump to navigation Jump to search

LaTeX

Getting Started
  1. Introduction
  2. Installation
  3. Installing Extra Packages
  4. Basics
  5. How to get help

Common Elements

  1. Document Structure
  2. Text Formatting
  3. Paragraph Formatting
  4. Colors
  5. Fonts
  6. List Structures
  7. Special Characters
  8. Internationalization
  9. Rotations
  10. Tables
  11. Title creation
  12. Page Layout
  13. Customizing Page Headers and Footers‎
  14. Importing Graphics
  15. Floats, Figures and Captions
  16. Footnotes and Margin Notes
  17. Hyperlinks
  18. Labels and Cross-referencing
  19. Initials

Mechanics

  1. Errors and Warnings
  2. Lengths
  3. Counters
  4. Boxes
  5. Rules and Struts

Technical Text

  1. Mathematics
  2. Advanced Mathematics
  3. Theorems
  4. Chemical Graphics
  5. Algorithms
  6. Source Code Listings
  7. Linguistics

Special Pages

  1. Indexing
  2. Glossary
  3. Bibliography Management
  4. More Bibliographies

Special Documents

  1. Scientific Reports (Bachelor Report, Master Thesis, Dissertation)
  2. Letters
  3. Presentations
  4. Teacher's Corner
  5. Curriculum Vitae
  6. Academic Journals (MLA, APA, etc.)

Creating Graphics

  1. Introducing Procedural Graphics
  2. MetaPost
  3. Picture
  4. PGF/TikZ
  5. PSTricks
  6. Xy-pic
  7. Creating 3D graphics

Programming

  1. Macros
  2. Plain TeX
  3. Creating Packages
  4. Creating Package Documentation
  5. Themes

Miscellaneous

  1. Modular Documents
  2. Collaborative Writing of LaTeX Documents
  3. Export To Other Formats

Help and Recommendations

  1. FAQ
  2. Tips and Tricks

Appendices

  1. Authors
  2. Links
  3. Package Reference
  4. Sample LaTeX documents
  5. Index
  6. Command Glossary

edit this boxedit the TOC

Always writing LaTeX in roman[edit | edit source]

If you insert the \LaTeX command in an area with a non-default font, it will be formatted accordingly. If you want to keep LaTeX written in Computer Modern roman shape, you must redefine the function. However, the naive

\renewcommand{\LaTeX}{{\rm \LaTeX}}

will output:

TeX capacity exceeded , sorry [ grouping levels =255].

So you need to create a temporary variable.

Sadly,

\newcommand{\LaTeXtemp}{\LaTeX}
\renewcommand{\LaTeX}{{\rm \LaTeXtemp}}

does not work either.

We must use the TeX primitive \let instead.

\let\LaTeXtemp\LaTeX
\renewcommand{\LaTeX}{{\rm \LaTeXtemp }}

id est and exempli gratia (i.e. and e.g.)[edit | edit source]

If you simply use the forms "i.e." or "e.g.", LaTeX will treat the periods as end of sentence periods (i.e. full stop) since they are followed by a space, and add more space before the next "sentence". To prevent LaTeX from adding space after the last period, the correct syntax is either "i.e.\ " or "e.g.\ ", or else "i.e., " and "e.g., " with a comma. The comma is interpreted by LaTeX as part of a sentence, since the period is not followed by any space. In this case, "i.e.," and "e.g.," do not need any special attention. The latter style is favored, e.g., by The Chicago Manual of Style.

If the command \frenchspacing is used in the preamble, the space between sentences is always the same as that between words.

Grouping Figure/Equation Numbering by Section[edit | edit source]

For long documents the numbering can become cumbersome as the numbers reach into double and triple digits. To reset the counters at the start of each section and prefix the numbers by the section number, include the following in the preamble.

\usepackage{amsmath}
\numberwithin{equation}{section}
\numberwithin{figure}{section}

The same can be done with similar counter types and document units such as "subsection".


Graphics and Graph editors[edit | edit source]

Vector image editors with LaTeX support[edit | edit source]

It is often preferable to use the same font and font size in your images as in the document. Moreover, for scientific images, you may need mathematical formulae or special characters (such as Greek letters). Both things can be achieved easily if the image editor allows you to use LaTeX code in your image. Most vector image editors do not offer this option. There are, however, a few exceptions.

In early days, LaTeX users used Xfig for their drawings. The editor is still used by quite a few people nowadays because it has special 'export to LaTeX' features. It also gives you some very basic ways of encapsulating LaTeX text and math in the image (setting the text's 'special flag' to 'special' instead of 'normal'). When exporting, all LaTeX text will be put in a .tex-file, separately from the rest of the image (which is put in a .ps file).

A newer and easier-to-use vector image editor specially tailored to LaTeX use is IPE. It allows any LaTeX command, including but not limited to mathematical formulae in the image. The program saves its files as editable .eps or .pdf files, which eliminates the need of exporting your image each time you have edited it.

A very versatile vector image editor is Inkscape. It does not support LaTeX text by itself, but you can use the plugin Textext for that. This allows you to put any block of LaTeX code in your image. Additionally since version 0.48 you can export to vectorgraphics with texts separated in a .tex file. Using this way text is rendered by the latex compiler itself.

LaTeXDraw is a free and open source graphical PSTricks generator and editor. It allows you to draw basic geometric objects and save the result in a variety of formats including .jpg, .png, .eps, .bmp as well as .tex. In the last case the saved file contains PSTricks/LaTeX code only. Owing to that you can include any possible LaTeX code in the picture, since the file is rendered by your LaTeX environment directly.

Another way to generate vectorgraphics is using the Asymptote language. It is a programming language which produces vector images in encapsulated postscript format and supports LaTeX syntax in any textlabels.

Graphs with gnuplot[edit | edit source]

A simple method to include graphs and charts in LaTeX documents is to create it within a common spreadsheet software (OpenOffice Calc or MS Office Excel etc.) and include it in the document as a cropped screenshot. However, this produces poor quality rasterized images. Calc also allows you to copy-paste the charts into OpenOffice Draw and save them as PDF files.

Using Microsoft Excel 2010, charts can be copied directly to Microsoft Expression Design 4, where they can be saved as PDF files. These PDF files can be included in LaTeX. This method produces high quality vectorized images.

An excellent method to render graphs is through gnuplot, a free and versatile plotting software, that has a special output filter directly for exporting files to LaTeX. We assume, that the data is in a CSV file (comma separated text) in the first and third column. A simple gnuplot script to plot the data can look like this:

gnuplot can plot various numerical data, functions, error distribution as well as 3D graphs and surfaces
set format "$%g$"
set title "Graph 3: Dependence of $V_p$ on $R_0$"
set xlabel "Resistance $R_0$ [$\Omega$]"
set ylabel "Voltage $V_p$ [V]"
set border 3
set xtics nomirror
set ytics nomirror
set terminal epslatex
set output "graph1.eps"
plot "graph1.csv" using 1:3   #Plot the data

Now gnuplot produces two files: the graph drawing in graph.eps and the text in graph.tex. The second includes the EPS image, so that we only need to include the file graph.tex in our document:

\input{graph1.tex}

The above steps can be automated by the package gnuplottex. By placing gnuplot commands inside \begin{gnuplot}\end{gnuplot}, and compiling with latex -shell-escape, the graphs are created and added into your document.

Failure to access gnuplot from latex for Windows can be solved by making file title only in one word. Don't type my report.tex for your title file, but do myreport.tex .

When you are using gnuplottex it is also possible to directly pass the terminal settings as an argument to the environment

 \begin{gnuplot}[terminal=epslatex, terminaloptions=color, scale=0.9, linewidth=2 ]
 ...
 \end{gnuplot}

Using gnuplottex can cause fraudulent text-highlighting in some editors when using algebraic functions on imported data, such as:

 (2*($1)):2

Some editors will think of all following text as part of a formula and highlight it as such (because of the '$' that is interpreted as part of the latex code). This can be avoided by ending with:

#$
\end{gnuplot}

As it uncomments the dollar sign for the gnuplot interpreter, but is not affecting the interpretation of the .tex by the editor.

When using pdfLaTeX instead of simple LaTeX, we must convert the EPS image to PDF and to substitute the name in the graph1.tex file. If we are working with a Unix-like shell, it is simply done using:

eps2pdf graph1.eps
sed -i s/".eps"/".pdf"/g graph1.tex

With the included tex file we can work as with an ordinary image.

Instead of calling eps2pdf directly, we can also include the epstopdf package that automates the process. If we include a graphics now and leave out the file extension, epstopdf will automatically transform the .eps-file to PDF and insert it in the text.

\includegraphics{graph1}

This way, if we choose to output to PS or DVI, the EPS version is used and if we output to PDF directly, the converted PDF graphics is used. Please note that usage of epstopdf requires compiling with latex -shell-escape.

Note: Emacs AucTex users might want to check out Gnuplot-mode.

Generate png screenshots[edit | edit source]

See Export To Other Formats.

Spell-checking and Word Counting[edit | edit source]

If you want to spell-check your document, you can use the command-line aspell, hunspell (preferably), or ispell programs.

ispell yourfile.tex
aspell --mode=tex -c yourfile.tex
hunspell -l -t -i utf-8 yourfile.tex

All three understand LaTeX and will skip LaTeX commands. You can also use a LaTeX editor with built-in spell checking, such as LyX, Kile, or Emacs. Another option is to convert LaTeX source to plain text and open resulting file in a word processor like OpenOffice.org or KOffice.

If you want to count words you can, again, use LyX or convert your LaTeX source to plain text and use, for example, UNIX wc command:

detex yourfile | wc

An alternative to the detex command is the pdftotext command which extracts an ASCII text file from PDF:

1. pdflatex yourfile.tex
2. pdftotext yourfile.pdf
3. wc yourfile.txt

New even page[edit | edit source]

In the twoside-mode you have the ability to get a new odd-side page by:

\cleardoublepage

However, LaTeX doesn't give you the ability to get a new even-side page. The following method opens up this;

The following must be put in your document preamble:

\usepackage{ifthen}

\newcommand{\newevenside}{
	\ifthenelse{\isodd{\thepage}}{\newpage}{
	\newpage
        \phantom{placeholder} % doesn't appear on page
	\thispagestyle{empty} % if want no header/footer
	\newpage
	}
}

To active the new even-side page, type the following where you want the new even-side:

\newevenside

If the given page is an odd-side page, the next new page is subsequently an even-side page, and LaTeX will do nothing more than a regular \newpage. However, if the given page is an even page, LaTeX will make a new (odd) page, put in a placeholder, and make another new (even) page. A crude but effective method.


Sidebar with information[edit | edit source]

If you want to put a sidebar with information like copyright and author, you might want to use the eso-pic package. Example:

\usepackage{eso-pic}
...
\AddToShipoutPicture{%
  \AtPageLowerLeft{%
    \rotatebox{90}{%
        \begin{minipage}{\paperheight}
          \centering\textcopyright~\today{} Humble me
        \end{minipage} %
      }
    } %
  }%

If you want it on one page only, use the starred version of the AddToShipoutPicture command at the page you want it. (\AddToShipoutPicture*{...})

Hide auxiliary files[edit | edit source]

If you're using pdflatex you can create a folder in which all the output files will be stored, so your top directory looks cleaner.

pdflatex -output-directory tmp

Please note that the folder tmp should exist. However if you're using a Unix-based system you can do something like this:

alias pdflatex='mkdir -p tmp; pdflatex -output-directory tmp'

Or for vim modify your .vimrc:

" use pdflatex
let g:Tex_DefaultTargetFormat='pdf'
let g:Tex_MultipleCompileFormats='pdf,dvi'
let g:Tex_CompileRule_pdf = 'mkdir -p tmp; pdflatex -output-directory tmp 
-interaction=nonstopmode $*; cp tmp/*.pdf .'


Previous: FAQ Index Next: Authors