LaTeX/Advanced Topics
From Wikibooks, the open-content textbooks collection
Here are some topics that are not really necessary to write a proper document, but could help you making your life easier and giving you some details to modify.
Contents |
[edit] Splitting the document into multiple files
As the typing goes on, your LaTeX file could get big and confusing, so it could be a good idea to split it into several other documents. For example, if you are writing a book, you could consider writing every chapter in its own .tex file. LaTeX makes this very easy thanks to the command:
\input{filename.tex}
While processing the document, the compiler will simply read the content of filename.tex and put it within the document instead of the input command. This way you can put all the formatting options in your "root" document and then input other files containing only text and very basic commands such as \section, etc. The code will be much cleaner and more readable.
Another method of including a file is to use \include{filename}. However, you cannot nest \include statements within a file added via \include, whereas you can using input. Note that this will also have an effect on the number of compiles you'll need to do.
[edit] Adding your own counters
In LaTeX it is fairly easy to create new counters and even counters that reset automatically when another counter is increased (think subsection in a section for example). With the command
\newcounter{TheNameForTheNewCounter}
you create a new counter that is automatically set to zero. If you want the counter to be reset to zero every time another counter is increased, use:
\newcounter{TheNameForTheNewCounter}[TheNameOfTheOtherCounter]
To increase the counter, either use
\stepcounter{TheNameForTheNewCounter}
or
\refstepcounter{TheNameForTheNewCounter} % used for labels and cross referencing
or
\addtocounter{TheNameForTheNewCounter}{number}
here the number can also be negative. For automatic reseting you need to use \stepcounter.
The values of the counters can be easily found by, for example:
\arabic{TheNameForTheNewCounter}
Instead of \arabic you could also use \alph, \Alph, \roman, or \Roman.
Here is an example for recreating something similar to a section and subsection counter that already exist in LaTeX:
\newcounter{mysection} \newcounter{mysubsection}[mysection] \addtocounter{mysection}{2} % set them to some other numbers than 0 \addtocounter{mysection}{10} % same % \arabic{mysection}.\arabic{mysubsection} bla bla \stepcounter{mysection} \arabic{mysection}.\arabic{mysubsection} bla bla \stepcounter{mysubsection} \arabic{mysection}.\arabic{mysubsection} bla bla \addtocounter{mysubsection}{25} \arabic{mysection}.\arabic{mysubsection} bla bla and more bla bla
[edit] Creating your own package
As you know, you can easily include any package by the command:
\usepackage{packagename}
but what if you want to create your own? It is very simple:
- create a simple text file called mypack.sty and open it with any text editor
- at the beginning of the text document just write
\ProvidesPackage{mypack}
note: it has to have the same name of the file without the extension
- write whatever you want in it using all the LaTeX commands you know. Normally you should define new commands or import other packages.
- import your new package with the known command
\usepackage{mypack}
the file mypack.sty and the LaTeX source you are compiling must be in the same directory. It will be like that all you have written within your package were within the document itself.
It is also possible to create your own style file. Similar to the creation of your own package, you can call your own style file in the preamble of any document by the command:
\documentclass{mystyle}
The name of the style file is then mystyle.cls and can be opened with any text editor. At the beginning of this file, the following lines have to be provided:
\ProvidesClass{mystyle}
again, withing the style files other files or packages are imported by the requirepackage command.
[edit] Using different paths
When referring to an external file in the LaTeX document source, if you just write a filename the compiler will look for it in the same directory of the source. In general you can refer to any document on your hardisk, using both relative and absolute paths. In general, you might want to make your source portable (to another computer or to a different location of you hardisk), so always use relative paths. A relative path is always defined in terms of the current directory where you are running the compiler, i.e. the directory where the source you are compiling is. LaTeX uses the standard *nix notation: with a simple dot . you refer to the current directory, and by two dots .. you refer to the previous directory, that is the upper one in the file system tree. By a slash / you can separate names of different directories. So by ./ you refer to the current directory, by ../ you refer to the previous directory, by ../../ you refer to two upper directories in the filesystem tree. Writing
\input{./filename.tex}
will have exactly the same effect as writing
\input{filename.tex}
but if you want to put all your files in a different directory called myfiles, you can refer to that file by
\input{./myfiles/filename.tex}
This lets you put your files wherever you want. Do not leave empty spaces in the filenames: they can cause ambiguous behavior. Use underscores _ instead.
It should be noted that LaTeX uses forward slashes / even when on a Microsoft Windows platform that normally uses backslashes \.
[edit] Using \includeonly
When writing a large document, it is sometimes useful to work on one section of the document. In LaTeX, the command
\includeonly{filename1, filename2, ...}
includes only the files specified between the brackets when used in the preamble of the document.
This requires that there are \include commands in the document specifying these files. The filename should be written without the .tex file extension
\include{filename1} \include{filename2}
[edit] Boxes
LaTeX builds up its pages by pushing around boxes. At first, each letter is a little box, which is then glued to other letters to form words. These are again glued to other words, but with special glue, which is elastic so that a series of words can be squeezed or stretched as to exactly fill a line on the page.
I admit, this is a very simplistic version of what really happens, but the point is that TeX operates on glue and boxes. Letters are not the only things that can be boxes. You can put virtually everything into a box, including other boxes. Each box will then be handled by LATEX as if it were a single letter.
In the past chapters you have already encountered some boxes, although I did not tell you. The tabular environment and the \includegraphics, for example, both produce a box. This means that you can easily arrange two tables or images side by side. You just have to make sure that their combined width is not larger than the \textwidth.
You can also pack a paragraph of your choice into a box with either the
\parbox[pos]{width}{text}
command or the
\begin{minipage}[pos]{width} text \end{minipage}
environment. The pos parameter can take one of the letters c, t or b to control the vertical alignment of the box, relative to the baseline of the surrounding text. width takes a length argument specifying the width of the box. The main difference between a minipage and a \parbox is that you cannot use all commands and environments inside a parbox, while almost anything is possible in a minipage.
While \parbox packs up a whole paragraph doing line breaking and everything, there is also a class of boxing commands that operates only on horizontally aligned material. We already know one of them; it’s called \mbox. It simply packs up a series of boxes into another one, and can be used to prevent LaTeX from breaking two words. As you can put boxes inside boxes, these horizontal box packers give you ultimate flexibility.
\makebox[width][pos]{text}
width defines the width of the resulting box as seen from the outside (This means it can be smaller than the material inside the box. You can even set the width to 0pt so that the text inside the box will be typeset without influencing the surrounding boxes). Besides the length expressions, you can also use \width, \height, \depth, and \totalheight in the width parameter. They are set from values obtained by measuring the typeset text. The pos parameter takes a one letter value: center, flushleft, flushright, or spread the text to fill the box.
The command \framebox works exactly the same as \makebox, but it draws a box around the text.
The following example shows you some things you could do with the \makebox and \framebox commands:
\makebox[\textwidth]{% c e n t r a l}\par \makebox[\textwidth][s]{% s p r e a d}\par \framebox[1.1\width]{Guess I’m framed now!} \par \framebox[0.8\width][r]{Bummer, I am too wide} \par \framebox[1cm][l]{never mind, so am I} Can you read this? |
Now that we control the horizontal, the obvious next step is to go for the vertical. No problem for LaTeX. The
\raisebox{lift}[extend-above-baseline][extend-below-baseline]{text}
command lets you define the vertical properties of a box. You can use \width, \height, \depth, and \totalheight in the first three parameters, in order to act upon the size of the box inside the text argument:
\raisebox{0pt}[0pt][0pt]{\Large% \textbf{Aaaa\raisebox{-0.3ex}{a}% \raisebox{-0.7ex}{aa}% \raisebox{-1.2ex}{r}% \raisebox{-2.2ex}{g}% \raisebox{-4.5ex}{h}}} he shouted but not even the next one in line noticed that something terrible had happened to him. |
[edit] Rules and Struts
The \rule command in normal use produces a simple black box:
\rule[lift]{width}{height}
Here is an example:
\rule{3mm}{.1pt}% \rule[-1mm]{5mm}{1cm}% \rule{3mm}{.1pt}% \rule[1mm]{1cm}{5mm}% \rule{3mm}{.1pt} |
This is useful for drawing vertical and horizontal lines.
A special case is a rule with no width but a certain height. In professional typesetting, this is called a strut. It is used to guarantee that an element on a page has a certain minimal height. You could use it in a tabular environment to make sure a row has a certain minimum height.
| Previous: Producing Mathematical Graphics | Index | Next: Customizing LaTeX |


