LaTeX/Macros

From Wikibooks, open books for an open world
Jump to: navigation, search
LaTeX logo.svg

LaTeX

Getting Started
  1. Introduction
  2. Installation
  3. Installing Extra Packages
  4. Basics

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. Importing Graphics
  14. Floats, Figures and Captions
  15. Footnotes and Margin Notes
  16. Hyperlinks
  17. Labels and Cross-referencing

Mechanics

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

Technical Texts

  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. Letters
  2. Presentations
  3. Teacher's Corner
  4. Curriculum Vitae

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

Documents produced with the commands you have learned up to this point will look acceptable to a large audience. While they are not fancy-looking, they obey all the established rules of good typesetting, which will make them easy to read and pleasant to look at. However, there are situations where LaTeX does not provide a command or environment that matches your needs, or the output produced by some existing command may not meet your requirements.

In this chapter, we will try to give some hints on how to teach LaTeX new tricks and how to make it produce output that looks different from what is provided by default.

LaTeX is a fairly high-level language compared to Plain TeX and thus is more limited. The next chapter will focus on Plain TeX and will explain advanced techniques for programming.

New commands[edit]

To add your own commands, use the

\newcommand{name}[num]{definition}

command. Basically, the command requires two arguments: the name of the command you want to create, and the definition of the command. Note that the command name can but need not be enclosed in braces, as you like. The num argument in square brackets is optional and specifies the number of arguments the new command takes (up to 9 are possible). If missing it defaults to 0, i.e. no argument allowed.

The following two examples should help you to get the idea. The first example defines a new command called \wbal that will print "The Wikibook about LaTeX". Such a command could come in handy if you had to write the title of this book over and over again.

\newcommand{\wbal}{The Wikibook about \LaTeX}
This is ‘‘\wbal'' \ldots{
} ‘‘\wbal''

This is “The Wikibook about LaTeX” … “The Wikibook about LaTeX”

The next example illustrates how to define a new command that takes one argument. The #1 tag gets replaced by the argument you specify. If you wanted to use more than one argument, use #2 and so on, these arguments are added in an extra set of brackets.

\newcommand{\wbalsup}[1] {
  This is the Wikibook about LaTeX
  supported by #1}
\newcommand{\wbalTwo}[2] {
  This is the Wikibook about LaTeX
  supported by #1 and #2
}
% in the document body:
\begin{itemize}
\item \wbalsup{Wikimedia}
\item \wbalsup{lots of users!}
\item \wbalTwo{John Doe}{Anthea Smith}
\end{itemize
}

  • This is the Wikibook about LaTeX supported by Wikimedia
  • This is the Wikibook about LaTeX supported by lots of users!
  • This is the Wikibook about LaTeX supported by John Doe and Anthea Smith

Name your new command \wbalTwo and not \wbal2 as digits cannot be used to name macros — invalid characters will error out at compile-time.

LaTeX will not allow you to create a new command that would overwrite an existing one. But there is a special command in case you explicitly want this: \renewcommand. It uses the same syntax as the \newcommand command.

In certain cases you might also want to use the \providecommand command. It works like \newcommand, but if the command is already defined, LaTeX will silently ignore the new command.

With LaTex2e, it is also possible to add a default parameter to a command with the following syntax:

\newcommand{name}[num][default]{definition}

If the default parameter of \newcommand is present, then the first of the number of arguments specified by num is optional with a default value of default; if absent, then all of the arguments are required.

\newcommand{\wbalTwo}[2][Wikimedia]{
  This is the Wikibook about LaTeX
  supported by {#1} and {#2}!
}
% in the document body:
\begin{itemize}
\item \wbalTwo{John Doe}
\item \wbalTwo[lots of users]{John Doe}
\end{itemize
}

  • This is the Wikibook about LaTeX supported by Wikimedia and John Doe!
  • This is the Wikibook about LaTeX supported by lots of users and John Doe!
Note
When the command is used with an explicit first parameter it is given enclosed with brackets (here "[lots of users]").

Here is a common example: if you are writing a book about Mathematics and you have to use vectors, you have to decide how they will look. There are several different standards, used in many books. If a is a vector, some people like to add an arrow over it (\vec{a}), other people write it underlined (a); another common version is to write it bold (a). Let us assume you want to write your vectors with an arrow over them; then add the following line in your mystyle.sty.

\newcommand{\myvec}[1]{\vec{#1}}

and write your vectors inside the new \myvec{...} command. You can call it as you wish, but you'd better choose a short name because you will probably write it very often. Then, if you change your mind and you want your vectors to look differently you just have to change the definition of your \myvec{...}. Use this approach whenever you can: this will save you a lot of time and increase the consistency of your document.

DeclareRobustCommand[edit]

Some commands are fragile, that is they fail in some environments. If a macro works in body text but not in (for example) a figure caption, it's worth trying to replace the \newcommand{\MyCommand}... declaration with \DeclareRobustCommand{\MyCommand}... in the preamble. This is especially true for macros which, when expanded, produce text that is written to a .aux file.

New environments[edit]

Just as with the \newcommand command, there is a command to create your own environments. The \newenvironment command uses the following syntax:

\newenvironment{name}[num]{before}{after}

Again \newenvironment can have an optional argument. When the \begin{name} command (which starts the environment) is encountered, the material specified in the before argument is processed before the text in the environment gets processed. The material in the after argument gets processed when the \end{name} command (which ends the environment) is encountered.

The num argument is used the same way as in the \newcommand command. LaTeX makes sure that you do not define an environment that already exists. If you ever want to change an existing command, you can use the \renewenvironment command. It uses the same syntax as the \newenvironment command.

The example below illustrates the usage of the \newenvironment command:

\newenvironment{king}
{ \rule{1ex}{1ex}\hspace{\stretch{1}} }
{ \hspace{\stretch{1}}\rule{1ex}{1ex} }

\begin{king}
My humble subjects \ldots
\end{king
}

Latex example newenvironment.png

Unmatched braces[edit]

Often, part of the motive behind creating an environment is to surround its body in a grouping of braces. However, this requires unmatched braces to appear in both the beginning and end portions of the environment declaration, which will prevent the document from compiling. To solve this issue, use the TeX synonyms \bgroup and \egroup instead of typing { and } in this case.

Extra space[edit]

When creating a new environment you may easily get bitten by extra spaces creeping in, which can potentially have fatal effects. For example when you want to create a title environment which suppresses its own indentation as well as the one on the following paragraph. The \ignorespaces command in the begin block of the environment will make it ignore any space after executing the begin block. The end block is a bit more tricky as special processing occurs at the end of an environment. With the \ignorespacesafterend LaTeX will issue an \ignorespaces after the special ‘end’ processing has occurred.

\newenvironment{simple}%
{\noindent}%
{\par\noindent}

\begin{simple}
See the space\\to the left.
\end{simple
}
Same\\here.

  See the space
to the left.

  Same
here.


\newenvironment{correct}%
{\noindent\ignorespaces}%
{\par\noindent%
\ignorespacesafterend}

\begin{correct}
No space\\to the left.
\end{correct
}
Same\\here.

No space
to the left.

Same
here.

Also, if you're still having problems with extra space being appended at the end of your environment when using the \input for external source, make sure there is no space between the beginning, sourcing, and end of the environment, such as:

\begin{correct}\input{somefile.tex}\end{correct}

or

\begin{correct}%
\input{somefile.tex}%
\end{correct}

Extending the number of arguments[edit]

The xkeyval packages will let you define key/value options for commands.

\mycommand[key1=value1, key3=value3]{some text}

The package is quite complete and documentation is exhaustive. We recommend that package developers read it. [1]

Let's provide a simple example[1]:

\usepackage{xkeyval}
% ...

\makeatletter
\def\my@emphstyle#1{\csname my@style@#1\endcsname}
%% Predefined styles
\providecommand\my@style@default{\em}
\providecommand\my@style@bold{\bfseries}

\define@key{myemph}{code
}{%
  \def\my@emphstyle{#1}
}
\define@key{myemph}{style
}{%
  \def\my@emphstyle{\csname my@style@#1\endcsname}
}
\newcommand\setemph[1]{%
  \setkeys{myemph}{#1}
}

\renewcommand\emph[1]{%
  {\my@emphstyle #1}
}

\makeatother

Something \emph{important}

\setemph{style=bold}
Something \emph{important}

\setemph{code=\Large\sffamily}
Something \emph{important
}

Arithmetic[edit]

LaTeX can manipulate numbers.

The calc package provides the common infix notation.

\usepackage{calc}
% ...
\newcounter{mine}
\setcounter{mine}{2*17
}
\themine

For high-precision computations, you can use the fp package.

\usepackage{fp}

% Clip
\[
\FPmul\result{2}{7}
\FPclip\result\result
2*7 = \result
\
]

% Infix
\[
\newcommand\result{11}
\sqrt{\sin(2+\result)} \approx
\FPeval\result{round(root(2,sin(result + \FPpi)):2)
}
\result
\
]

% Postfix
\[
\FPupn\result{17 2.5 + 17.5 - 2 1 + * 2 swap /}
\FPclip\result\result
(17+2.5 - 17.5) * (2+1) / 2  = \result
\
]

% High precision
\[
\FPdiv\result{17}{7}
\frac{17}{7} \approx \FPtrunc\result\result{3
}
\result
\
]

Conditionals[edit]

LaTeX can use conditionals thanks to the ifthen package.

\usepackage{ifthen}
% ...

\ifthenelse{ \equal{\myvar}{true} }{
  This is true.
}{
  This is false.
}

Loops[edit]

The PGF/TikZ extension provides the \foreach command.

\usepackage{tikz}
% ...

\foreach \i/\q in {wheat/50g, water/1L, yeast/2g}{
  \noindent\i\dotfill\q\\
}

Alternatively you can check out the multido package.

Strings[edit]

xstring provides a lot of features. From CTAN:

  • testing a string’s contents
  • extracting substrings
  • substitution of substrings
  • string length
  • position of a substring
  • number of recurrences of a substring

Examples:

\usepackage{xstring}
% ...

\newcommand\mystr{Hello World!}

The string ``\mystr'' has \StrLen{\mystr}{} characters.

Predicate ``\mystr{} contains the word Hello'' is \IfSubStr{\mystr}{Hello}{true}{false
}.

LaTeX Hooks[edit]

LaTeX provide two hooks:

  • \AtBeginDocument will let you specify a set of commands that will be executed when \begin{document} is met.
  • \AtEndDocument does the same for \end{document}.

This gives you some more flexiblity for macros. It can be useful to override settings that get executed after the preamble. These hooks can be called several times. The commands will be executed in the order they were set.

For instance, let's replace the page numbers with oldstylenums:

\usepackage{textcomp}

\AtBeginDocument{%
  % Make the page numbers in text figures
  \let\myThePage\thepage
  \renewcommand{\thepage}{ \oldstylenums{\myThePage} }
}

There are also hooks for classes and packages. See Creating Packages.

Command-line LaTeX[edit]

If you work on a Unix-like OS, you might be using Makefiles or any kind of script to build your LaTeX projects. In that connection it might be interesting to produce different versions of the same document by calling LaTeX with command-line parameters. If you add the following structure to your document:

\usepackage{ifthen}
%...

% default value.
\providecommand\blackandwhite{false}
%...

\ifthenelse{ \equal{\blackandwhite}{true} }{
% "black and white" mode; do something..
}{
% "color" mode; do something different..
}

Now you can call LaTeX like this:

latex '\providecommand{\blackandwhite}{true}\input{test.tex}'

First the command \blackandwhite gets defined and then the actual file is read with input. By setting \blackandwhite to false the color version of the document would be produced.

Notes and References[edit]


Previous: Creating 3D graphics Index Next: Plain TeX