LaTeX: Difference between revisions
Jump to navigation
Jump to search
(14 intermediate revisions by the same user not shown) | |||
Line 1: | Line 1: | ||
LaTeX is a fantastic typesetting package. Unfortunately it has a big problem: not a lot useful documentation. So this is a collection of useful tips I have learnt while spending a lot of time googling for LaTeX documentation. | LaTeX is a fantastic typesetting package. Unfortunately it has a big problem: not a lot of useful documentation. So this is a collection of useful tips I have learnt while spending a lot of time googling for LaTeX documentation. | ||
= Documentation = | = Documentation = | ||
Line 11: | Line 11: | ||
[http://www-h.eng.cam.ac.uk/help/tpl/textprocessing/teTeX/latex/latex2e-html/ltx-2.html Good resource site.] | [http://www-h.eng.cam.ac.uk/help/tpl/textprocessing/teTeX/latex/latex2e-html/ltx-2.html Good resource site.] | ||
[http://www.cv-templates.info/ Excellent site for making LaTeX resumes.] | |||
= Basic Concepts = | = Basic Concepts = | ||
Line 19: | Line 20: | ||
* References are defined via the \label command and used via the \ref command. If you plan to reference a section, use \label{sec:mysection}. For a table, use \label{tab:mytable}. Be careful though, for a table *all* \label commands must occur after the \caption command. | * References are defined via the \label command and used via the \ref command. If you plan to reference a section, use \label{sec:mysection}. For a table, use \label{tab:mytable}. Be careful though, for a table *all* \label commands must occur after the \caption command. | ||
== Document class == | |||
* Each LaTeX document must have a base class. Some options are defined when you declare the class; once set, you cannot change those in the middle of the document later. | |||
== Encodings == | |||
* There are two different concepts there. The first is the encoding of the source document; this will tell LaTeX how to map some characters to LaTeX primitive characters (eg, é -> e with acute). UTF-8 should be always used for that with the inputenc package: | |||
\usepackage[utf8]{inputenc} | |||
* The second concept is the font encoding; how will LaTeX produce a correct PDF? If you typeset French documents (and perhaps others), the following command should always be used: | |||
\usepackage[T1]{fontenc} | |||
This allows LaTeX to correctly hyphenate words with accents, along with creating correct searchable PDFs. | |||
* Note however that by default with \usepackage[T1]{fontenc}, bitmap fonts will be used and this will result in ugly PDFs. Two solutions: add '''\usepackage{lmodern}''' or install (under Gentoo) texlive-fontsextra which contain a better version of the CM fonts (with scalable glyphs for T1 as a font encoding). | |||
= Packages = | = Packages = | ||
* The '''url''' package allows you to output URLs in a better looking way. Just use the \url{} command. | * The '''url''' package allows you to output URLs in a better looking way. Just use the \url{} command. | ||
* The '''changepage''' package (previously chngpage) allows you to change the width of the page momentarily, thus reducing (or increasing) margins. | * The '''hyperref''' package is better than the '''url''' one. First it allows you to use UTF-8 links, and second, it creates actual links in Acrobat Reader (there are also inner links in the table of contents with this package). However you need to use it like this to avoid ugly default boxes: | ||
* The '''graphicx''' package seems necessary to deal with images, import it like this: | |||
\usepackage[colorlinks, urlcolor=black, linkcolor=black]{hyperref} | |||
* The '''changepage''' package (previously chngpage) allows you to change the width of the page momentarily, thus reducing (or increasing) margins. You can then use the \adjustwidth environment, or use the following command: | |||
\changepage{3cm}{}{}{}{}{-1.5cm}{}{}{} | |||
\changepage{textheight}{textwidth}{evensidemargin}{oddsidemargin}{columnsep}{topmargin}{headheight}{headsep}{footskip} | |||
It has 9 arguments, and all are relative. Don't forget to get back to normal layout later by issuing the reverse command: | |||
\changepage{-3cm}{}{}{}{}{1.5cm}{}{}{} | |||
* The '''graphicx''' package seems necessary to deal with images (such as using \includegraphics), import it like this: | |||
\usepackage[pdftex]{graphicx} | \usepackage[pdftex]{graphicx} | ||
* The '''enumitem''' package allows you to create | * The '''enumitem''' package allows you to create custom lists. In particular you can remove the indent produced by a normal list (which is useful for example in a table). | ||
* The colortbl package can be used to have colored rows in a table. To specify a color in RGB, use it like this: | |||
\rowcolor[rgb]{0.7,0.7,0.7} % You can change the color of text by using for example: \color{white}My text. | |||
* To write a document in French, use the following code: | * To write a document in French, use the following code: | ||
Line 46: | Line 81: | ||
which will negate the effects of French Babel on item lists. | which will negate the effects of French Babel on item lists. | ||
* '''colortbl''' produces some problems with the '''\multicolumn''' command. The text inside the \multicolumn command must not contain any newlines, which prevents you from having more than one paragraph in the table cell. | |||
= Environments = | = Environments = | ||
Line 55: | Line 92: | ||
== Tables and Boxes == | == Tables and Boxes == | ||
* You can generate a tabular environment with \begin{tabular}. You give the definitions of the columns in the mandatory argument. Note that borders are created only if you enter "|" characters in the column definition area. | * You can generate a tabular environment with '''\begin{tabular}'''. You give the definitions of the columns in the mandatory argument. Note that borders are created only if you enter "|" characters in the column definition area. | ||
* The tabular is usually included in a floating table environment. So the code would be like: | * The tabular is usually included in a floating table environment. So the code would be like: | ||
Line 68: | Line 105: | ||
* You cannot use a footnote inside a table. The best alternative is to use the ''threeparttable'' package, with the \tnote command and '''tablenotes''' environment. | * You cannot use a footnote inside a table. The best alternative is to use the ''threeparttable'' package, with the \tnote command and '''tablenotes''' environment. | ||
* The command \setlength{\extrarowheight}{0.2cm} allows you to set additional height for the table rows (only at the top though...). It needs the '''array''' package. | * The command '''\setlength{\extrarowheight}{0.2cm}''' allows you to set additional height for the table rows (only at the top though...). It needs the '''array''' package. | ||
* \renewcommand{\arraystretch}{1.7} allows you to define margins between rows at 70% more than normal. This works for top and bottom and can be useful, this applies to all columns though. Don't forget also to get back to normal stretch after the table is finished. | |||
* To change the normal alignment of cells for the table headers, you can use the \multicolumn command like this: | * To change the normal alignment of cells for the table headers, you can use the \multicolumn command like this: | ||
Line 75: | Line 113: | ||
\begin{tabular}{|c|r|r|r|r|r|} | \begin{tabular}{|c|r|r|r|r|r|} | ||
\hline | \hline | ||
Année & \multicolumn{1}{c|}{ | Année & \multicolumn{1}{c|}{2001} & \multicolumn{1}{c|}{2002} & | ||
\multicolumn{1}{c|}{ | \multicolumn{1}{c|}{2003} & \multicolumn{1}{c|}{2004} & \multicolumn{1}{c|}{2005}\\ | ||
\hline | \hline | ||
</pre> | </pre> | ||
* To add a partial horizontal line, use \cline{i-j}, where i and j are the column numbers (i may be equal to j). | * Note that \multicolumn should also be used when you want to define a cell spanning multiple columns. The first argument to the command is the number of columns. | ||
* Use \rule{0pt}{0.7cm} to add a vertical margin to a single row (here it would add 0.7cm). | * To add a partial horizontal line, use '''\cline{i-j}''', where i and j are the column numbers (i may be equal to j). | ||
* Use '''\rule{0pt}{0.7cm}''' to add a vertical margin to a single row (here it would add 0.7cm). This in fact has the effect of adding an invisible line, and can help you in tricky situations. To show the line (which can be helpful) you can change the 0pt to some positive value, e.g. 100 pt. There is also a very helpful first optional arguments, you can try things like: | |||
\rule[2cm]{0pt}{0.7cm} or \rule[-1.5cm]{0pt}{0.7cm} | |||
== Bibliography == | == Bibliography == | ||
Line 93: | Line 134: | ||
* The letter class is not recommended, because it does not allow you to use sections, subsections, etc. The article class can be used as a general purpose class, it's not mandatory to have a title, abstract or bibliography. | * The letter class is not recommended, because it does not allow you to use sections, subsections, etc. The article class can be used as a general purpose class, it's not mandatory to have a title, abstract or bibliography. | ||
* The letter class is suitable to one page letters or such documents though. | |||
* Using \maketitle usually inserts a date, but you can get the date to be blank if you use \date{}. | * Using \maketitle usually inserts a date, but you can get the date to be blank if you use \date{}. | ||
Line 113: | Line 155: | ||
* To use the Euro symbol, use \usepackage[official]{eurosym}. You can then write the symbol with the command \euro, or use \EUR{50} to directly write a monetary amount. | * To use the Euro symbol, use \usepackage[official]{eurosym}. You can then write the symbol with the command \euro, or use \EUR{50} to directly write a monetary amount. | ||
* To add a bullet symbol without using \begin{itemize}, use '''\bullet'''. This can be useful in tabulars for example (in this case write '''$\bullet$'''). You may want to add '''\,''' after '''\bullet''' to produce the horizontal space normally automatically added when using {itemize}. | |||
== Font size and style == | == Font size and style == | ||
Line 232: | Line 275: | ||
= Issues = | = Issues = | ||
* If you include a .png file and compile with pdf2latex, the resulting PDF file will be perfect under kpdf but NOT on Adobe Acrobat Reader (version 8). This is probably a bug in Reader, however this does not happen with .jpg files so if you need high quality, | * If you include a .png file and compile with pdf2latex, the resulting PDF file will be perfect under kpdf but NOT on Adobe Acrobat Reader (version 8). This is probably a bug in Reader, however this does not happen with .jpg files so if you need high quality, JPEG should be used. |
Latest revision as of 14:11, 3 November 2010
LaTeX is a fantastic typesetting package. Unfortunately it has a big problem: not a lot of useful documentation. So this is a collection of useful tips I have learnt while spending a lot of time googling for LaTeX documentation.
Documentation
- I found the following tutorials, books or cheatsheets useful:
- The Not So Short Introduction to LaTeX 2e (general introduction).
- TeX for the impatient (more complete book).
- Latex cheat sheet (very useful list of commands).
- They are free and available online.
Good resource site. Excellent site for making LaTeX resumes.
Basic Concepts
- In LaTeX there are environments (started with the \begin command) and commands. These are separate concepts although sometimes they seem to overlap. For example you create a tabular environment but you give a command to generate a paragraph box (\parbox).
- Sometimes you have to protect commands with the \protect command (which will apply to the next command). This happens when you have commands inside a caption for example. Note that the \\ (for a line break) also have to be protected.
- References are defined via the \label command and used via the \ref command. If you plan to reference a section, use \label{sec:mysection}. For a table, use \label{tab:mytable}. Be careful though, for a table *all* \label commands must occur after the \caption command.
Document class
- Each LaTeX document must have a base class. Some options are defined when you declare the class; once set, you cannot change those in the middle of the document later.
Encodings
- There are two different concepts there. The first is the encoding of the source document; this will tell LaTeX how to map some characters to LaTeX primitive characters (eg, é -> e with acute). UTF-8 should be always used for that with the inputenc package:
\usepackage[utf8]{inputenc}
- The second concept is the font encoding; how will LaTeX produce a correct PDF? If you typeset French documents (and perhaps others), the following command should always be used:
\usepackage[T1]{fontenc}
This allows LaTeX to correctly hyphenate words with accents, along with creating correct searchable PDFs.
- Note however that by default with \usepackage[T1]{fontenc}, bitmap fonts will be used and this will result in ugly PDFs. Two solutions: add \usepackage{lmodern} or install (under Gentoo) texlive-fontsextra which contain a better version of the CM fonts (with scalable glyphs for T1 as a font encoding).
Packages
- The url package allows you to output URLs in a better looking way. Just use the \url{} command.
- The hyperref package is better than the url one. First it allows you to use UTF-8 links, and second, it creates actual links in Acrobat Reader (there are also inner links in the table of contents with this package). However you need to use it like this to avoid ugly default boxes:
\usepackage[colorlinks, urlcolor=black, linkcolor=black]{hyperref}
- The changepage package (previously chngpage) allows you to change the width of the page momentarily, thus reducing (or increasing) margins. You can then use the \adjustwidth environment, or use the following command:
\changepage{3cm}{}{}{}{}{-1.5cm}{}{}{} \changepage{textheight}{textwidth}{evensidemargin}{oddsidemargin}{columnsep}{topmargin}{headheight}{headsep}{footskip}
It has 9 arguments, and all are relative. Don't forget to get back to normal layout later by issuing the reverse command:
\changepage{-3cm}{}{}{}{}{1.5cm}{}{}{}
- The graphicx package seems necessary to deal with images (such as using \includegraphics), import it like this:
\usepackage[pdftex]{graphicx}
- The enumitem package allows you to create custom lists. In particular you can remove the indent produced by a normal list (which is useful for example in a table).
- The colortbl package can be used to have colored rows in a table. To specify a color in RGB, use it like this:
\rowcolor[rgb]{0.7,0.7,0.7} % You can change the color of text by using for example: \color{white}My text.
- To write a document in French, use the following code:
\usepackage[francais]{babel}
- You can change the name given to the table captions by using the command:
\renewcommand{\tablename}{T\scriptsize{ABLEAU}\normalsize}
- The package a4wide makes horizontal margins smaller. It can be useful for some types of documents.
Conflicts
- The enumitem package conflicts with [francais]{babel}. You must use the latest frenchb.ldf file (the one in TexLive 2007, installed by Gentoo, is not new enough), and place \usepackage{enumitem} after \usepackage[francais]{babel}. I also had to use the following command:
\frenchbsetup{ReduceListSpacing=false,CompactItemize=false}
which will negate the effects of French Babel on item lists.
- colortbl produces some problems with the \multicolumn command. The text inside the \multicolumn command must not contain any newlines, which prevents you from having more than one paragraph in the table cell.
Environments
titlepage
- This environment allows you to write whatever you want as a cover page for the title.
Tables and Boxes
- You can generate a tabular environment with \begin{tabular}. You give the definitions of the columns in the mandatory argument. Note that borders are created only if you enter "|" characters in the column definition area.
- The tabular is usually included in a floating table environment. So the code would be like:
\begin{table}[h!] \begin{tabular}{|p{4cm}|c|l|} ... \end{tabular} \end{table}
- You cannot use a footnote inside a table. The best alternative is to use the threeparttable package, with the \tnote command and tablenotes environment.
- The command \setlength{\extrarowheight}{0.2cm} allows you to set additional height for the table rows (only at the top though...). It needs the array package.
- \renewcommand{\arraystretch}{1.7} allows you to define margins between rows at 70% more than normal. This works for top and bottom and can be useful, this applies to all columns though. Don't forget also to get back to normal stretch after the table is finished.
- To change the normal alignment of cells for the table headers, you can use the \multicolumn command like this:
\begin{tabular}{|c|r|r|r|r|r|} \hline Année & \multicolumn{1}{c|}{2001} & \multicolumn{1}{c|}{2002} & \multicolumn{1}{c|}{2003} & \multicolumn{1}{c|}{2004} & \multicolumn{1}{c|}{2005}\\ \hline
- Note that \multicolumn should also be used when you want to define a cell spanning multiple columns. The first argument to the command is the number of columns.
- To add a partial horizontal line, use \cline{i-j}, where i and j are the column numbers (i may be equal to j).
- Use \rule{0pt}{0.7cm} to add a vertical margin to a single row (here it would add 0.7cm). This in fact has the effect of adding an invisible line, and can help you in tricky situations. To show the line (which can be helpful) you can change the 0pt to some positive value, e.g. 100 pt. There is also a very helpful first optional arguments, you can try things like:
\rule[2cm]{0pt}{0.7cm} or \rule[-1.5cm]{0pt}{0.7cm}
Bibliography
- A simple bibliography can be produced with the environment thebibliography. Its usage is documented well in The Not So Short Introduction to LaTeX 2e.
- For larger projects, you may need to use BibTeX.
Techniques
Choosing a document class
- The letter class is not recommended, because it does not allow you to use sections, subsections, etc. The article class can be used as a general purpose class, it's not mandatory to have a title, abstract or bibliography.
- The letter class is suitable to one page letters or such documents though.
- Using \maketitle usually inserts a date, but you can get the date to be blank if you use \date{}.
Spacing and Page breaks
- Use \pagebreak to make a page break.
- Use \noindent to avoid the indentation of a paragraph.
- Use the following code to make a perfectly blank page (useful on a PDF for the cover page, when printed in duplex):
\pagebreak \thispagestyle{empty} \addtocounter{page}{-1} \hrule height 0pt \pagebreak
- Use \vspace (LaTeX command) rather than \vskip (TeX primitive). If you use \vspace at the top of the page, it seems you must use \vspace*{4cm}.
- Use ~ to force a non-breaking space. If you just need a space after for example the \ldots command, you may just use \ldots\ or \ldots{} .
Symbols
- To use the Euro symbol, use \usepackage[official]{eurosym}. You can then write the symbol with the command \euro, or use \EUR{50} to directly write a monetary amount.
- To add a bullet symbol without using \begin{itemize}, use \bullet. This can be useful in tabulars for example (in this case write $\bullet$). You may want to add \, after \bullet to produce the horizontal space normally automatically added when using {itemize}.
Font size and style
- Use the commands \tiny, \large, \Large, \huge to change the font size.
- Use {\bf } for boldface, {\it } for italics. However, a more modern and better way would be to use \textbf{} or \textit{}. Note that you can also use \emph{}.
Alignment
- By default LaTeX justifies a paragraph. Should you need to have it aligned use:
\begin{flushright} This is some text. \end{flushright}
- There is also a \begin{flushleft} environment, of course. To center a block, use the \begin{center} command.
- You can align a section title like this:
\section*{\begin{center}STATUTS\end{center}}
- \begin{flushright} starts a new paragraph. Sometimes you don't want that happening, in which case you can use the \raggingleft command which does the same thing (warning: the left/right arguments are inversed from flush).
Tables and Boxes
- The \parbox command allows you to create a paragraph box (eg, it will be in paragraph mode, which means its content can span several lines). This command takes a mandatory width argument, and an optional one controlling the vertical alignment of the contents.
Beamer
- Beamer is a LaTeX package allowing you to create powerful presentations. It will output PDF files.
- You create slides by using the \frame command. You should use the normal \section and \subsection commands which will create a normal table of contents. In some styles, the table of contents, or the current section / subsection will be displayed in all slides (in a left frame, or at the top, for example).
- The style can be very easily changed globally with a single command
\usetheme{Frankfurt}
- To pause the presentation, use the \pause command. This will actually create a new page in the resulting PDF.
- To highlight some important text, you can use \alert{}.
- You can use \begin{block} or \begin{beamerboxesrounded}{} to create nice frames around some blocks of text. If you use \begin{beamerboxesrounded}{}, don't forget the empty {} (for a non-existent title).
Tools
Viewer
- My favorite viewer when writing LaTeX files is kpdf. It automatically reloads changes to the underlying document which is very nice.
LaTeX Distribution
- As of 2008, the best available distribution for Linux/UNIX seems to be TexLive (version 2007, 2008 is not released yet). It has a stable ebuild for Gentoo Linux (the old tetex distribution is now obsolete).
Eclipse Support (via Texlipse)
- The support is good overall. It allows you to mix freely *.tex files within another project (with PHP files for example). It will configure a LaTeX builder for the project, which will build your document. However, it is intended that only one document is created (although you can combine multiple source TeX files to create it).
- Check the project properties to define which is the main LaTeX source file and which document will be built. You can also define what program will be used for the build (pdflatex etc).
Code Examples
- Two columns layout:
\begin{tabular}[t]{lr} \parbox[t]{5cm}{11 bis, rue de Genève\\ 91140 Villebon-sur-Yvette\\ France\\ (33) 09.52.29.23.58 \\ (33) 06.28.33.13.68} & \parbox[t]{7cm} { \raggedleft \url{jean-noel.rivasseau@polytechnique.org}\\ \url{http://www.elvanor.net/}\\ \url{http://www.shoopz.com/} } \end{tabular}
- Inclusion of an image, while reducing the page's margins for this image (you need to import changepage package):
\begin{figure}[t] \begin{adjustwidth}{-4cm}{-4cm} \begin{center} \includegraphics[width=18cm]{kameleon-1} \end{center} \end{adjustwidth} \caption{Write your caption here.} \end{figure}
- Use of threeparttable package:
\begin{table} \begin{adjustwidth}{-2cm}{-2cm} \begin{center} \begin{threeparttable} \begin{tabular}{|p{4cm}|p{4cm}|p{6cm}|} \hline {\bf Description} & {\bf Examples} & {\bf Intérêt}\\ \hline Réseaux sociaux & Facebook, LinkedIn, Vidaeo, Odnoklassniki & Aucun\tnote{1}\\ \hline \end{tabular} \begin{tablenotes} \item [1] Voici un semblant de footnote. \end{tablenotes} \end{threeparttable} \end{center} \end{adjustwidth} \begin{center} \caption{Troisième segment \end{center}
Issues
- If you include a .png file and compile with pdf2latex, the resulting PDF file will be perfect under kpdf but NOT on Adobe Acrobat Reader (version 8). This is probably a bug in Reader, however this does not happen with .jpg files so if you need high quality, JPEG should be used.