LaTeX/Advanced Topics
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.
[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{NameOfTheNewCounter}
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{NameOfTheNewCounter}[NameOfTheOtherCounter]
To increase the counter, either use
\stepcounter{NameOfTheNewCounter}
or
\refstepcounter{NameOfTheNewCounter} % used for labels and cross referencing
or
\addtocounter{NameOfTheNewCounter}{number}
here the number can also be negative. For automatic reseting you need to use \stepcounter.
To set the counter value explicitly, use
\setcounter{NameOfTheNewCounter}{number}
The values of the counters can be easily found by, for example:
\arabic{NameOfTheNewCounter}
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{mysubsection}{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] 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.
Admittedly, this is a very simplistic description of what really happens, but the point is that TeX operates with glue and boxes. Letters are not the only things that can be boxes. One 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.
The past chapters have already dealt with some boxes, although they weren't described as such. The tabular environment and the \includegraphics, for example, both produce a box. This means that one 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. |
An alternative to these approaches is the usage of the framed environment (you will need to include the "framed" package to use it). This provides an easy way to box a paragraph within a document:
\begin{framed} This is an easy way to box text within a document! \end{framed}
[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.