LaTeX/Tips and Tricks

From testwiki
Revision as of 20:38, 5 February 2008 by 129.25.22.173 (talk) (New Square Root: italics to <tt> for commands)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to navigation Jump to search

Template:LaTeX/Navigation

Add the Bibliography to the Table of Contents

If you are writing a book or report, you'll likely insert your bibliography using something like:

\begin{thebibliography}{99}
\bibitem{bib:one_book} some information
\bibitem{bib:one_article} other information
\end{thebibliography}

This will create a chapter-like output showing properly all your references. Anyway, even if it looks like a chapter, it will not be handled like that so it will not appear on the Table of Contents at the beginning of the document. If you want your bibliography to be in the table of contents, just add the following two lines just before the thebibliography environment:

\clearpage
\addcontentsline{toc}{chapter}{Bibliography}

The first line just terminates the current paragraph and page. If you are writing a book, you'd better use \cleardoublepage. The second line will add a line in the Table of Contents (first option, toc), it will be like the ones created by chapters (second option, chapter), and the third argument will be printed on the corresponding line in the Table of Contents; here Bibliography was chosen because it's the same text the thebibliography environment will automatically write when you use it, but you are free to write whatever you like.

This trick is particularly useful when you have to insert the bibliography in the Table of Contents, but it can work for anything. When LaTeX finds the code above, it will record the info as described and the current page number, inserting a new line in the Contents page.


Add the Bibliography to the Table of Contents as numbered item

If you instead want bibliography to be numbered section or chapter, you'll likely use this way:

\clearpage
\section{Bibliography}

\renewcommand*{\refname}{}
\begin{thebibliography}{99}

This will define heading of bibliography to be empty, so you can start normal section before bibliography.

id est & exempli gratia (i.e. & e.g.)

If you simply use the forms

i.e.

or

e.g.

LaTeX will treat the periods as end of sentence periods. The correct syntax is

i.e.\

or

e.g.\

which tells LaTeX not to consider the period as an end of sentence period. This syntax results in a shorter space.

Note:

  1. In Chicago style, "i.e." and "e.g." are almost always followed by a comma.
  2. If the command \frenchspacing has been given in the preamble, the space between sentences is already short.

Referencing Figures or Equations

A reference to a figure/equation is normally done using the \ref{}-command.

The result is shown in Figure \ref{fig:result}.

To avoid that line break separated "Figure" and "\ref" use "~" to glue the reference to the description:

The result is shown in Figure~\ref{fig:result}.

New Square Root

While writing Mathematics, some people prefer writing the square root "closing" it over its content. This way it will look clearer what is inside the square root and what is not. This habit is not normally used while writing with the computer because the text is supposed to be clear anyway, but if you want to change the output of the square root anyway, LaTeX gives you this possibility. Just add the following code at the beginning of your document, where you would place a \usepackage{...} command:

% New definition of square root:
% it renames \sqrt as \oldsqrt
\let\oldsqrt\sqrt
% it defines the new \sqrt in terms of the old one
\def\sqrt{\mathpalette\DHLhksqrt}
\def\DHLhksqrt#1#2{%
\setbox0=\hbox{$#1\oldsqrt{#2\,}$}\dimen0=\ht0
\advance\dimen0-0.2\ht0
\setbox2=\hbox{\vrule height\ht0 depth -\dimen0}%
{\box0\lower0.4pt\box2}}
The new style is on left, the old one on right

This is a TeX code that first renames the \sqrt command as \oldsqrt, then redefines \sqrt in terms of the old one, adding something more. The new square root can be seen in the picture on the right, compared to the old one. Unfortunately this code won't work if you want to use multiple roots: if you try to write ab as \sqrt[b]{a} after you used the code above, you'll just get a wrong output. In other words, you can redefine the square root this way only if you are not going to use multiple roots in the whole document.

A new oiint command

If you are writing about Mathematics you might need a symbol similar to \oint but with the double integral within the circle. Some LaTeX packages (e.g. esint) provide this symbol by the command \oiint, but using such a package usually affect the output of all the formulas within your document. Since you want to keep the elegant style of LaTeX, you'd better find a way to have a new \oiint without using such packages. The only way to do it is by defining it by your own. The best approach would be to define another command using basic TeX, but by LaTeX is more straightforward. Note that we will not use the AMSmath package, so everything will work in any LaTeX document.

The symbols we will use are the integral \int and a big circle \bigcirc . Moreover we have to be able to move those symbols on the right or on the left; the command \hspace{1cm} will introduce an horizontal space of 1 cm, it is possible to introduce negative spaces, thus moving an object on the left. We'll get the symbol we want with the following command:

\bigcirc \hspace{-0.55cm} \int \hspace{-0.25cm} \int

The value of the negative spaces to add has been chosen after several attempts; anyway it might not work properly for the particular font or compiler you are using. Make some tests to find the best values for you. The \int \hspace{-0.25cm} \int creates the symbol of a double integral (we didn't use the standard \iint because we wanted the two integrals closer). The \bigcirc \hspace{-0.55cm} creates the circus and moves the integral over it.

Obviously you don't want to type so much code whenever you need such a symbol, so just define the new command \oiint:

\newcommand{\oiint}{\bigcirc \hspace{-0.55cm} \int \hspace{-0.25cm} \int}

Put this before the \begin{document} of your document and you can use \oiint whenever you want in Math mode. Since the last symbol within the definition is an integral, you can freely add subscripts and superscripts, they will be handled just like on the basic symbol of the integral according to the settings you are using.

Generic header

As explained in the previous sections, a LaTeX source can be used to generate both a DVI and a PDF file. For very basic documents the source is the same but, if the documents gets more complicated, it could be necessary to make some changes in the source so that it will work for a format but it will not for the other. For example, all that is related to graphics has to be adapted according to the final format. As discussed in the section about floating objects, even if you should use different pictures according to the final format, you can override this limit putting in the same folder pictures in different formats (e.g., EPS and PNG) with the same name and link them without writing the extension. There is a simple way to solve this problem. Add the following text just after \documentclass[...]{...} :

\newif\ifpdf
\ifx\pdfoutput\undefined
  \pdffalse
\else
  \ifnum\pdfoutput=1
    \pdftrue
  \else
    \pdffalse
  \fi
\fi

this is plain TeX code. It defines a new if-else you can use to change your code according to the compiler you are using. After you have used this code, you can use whenever you want in your document the following syntax:

\ifpdf
    % we are running pdflatex
\else
    % we are running latex
\fi

place after \ifpdf the code you want to insert if you are compiling with pdflatex, place after \else the code you want to insert if you are compiling with latex. For example, you can use this syntax to load different packages according to the compiler.

Using graphs from GNUPlot

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.

It is much better is to render or draw the graphs in some vector image editor, like Inkscape or Xfig. But even this way does not allow us to use the same font and text size as the rest of the document has, not mentioning usage of mathematical formulae in the legend.

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}

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 usual image.

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

Template:Latex/Navigation