LaTeX/Customizing LaTeX

From Wikibooks, open books for an open world
Jump to: navigation, search

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

Contents

[edit] New commands

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. 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 #2}
% in the document body:
\begin{itemize}
\item \wbalsup{Wikimedia}
\item \wbalsup{lots of users!}
\item \wbalTwo{John}{Doe}
\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

Note: use \wbalTwo, not \wbal2 (error on compiling)

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


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 ( "[lots of users]" ).

[edit] New Environments

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

[edit] Extra space

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}

[edit] Command-line LaTeX

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}
\ifthenelse{\equal{\blackandwhite}{true}}{
% "black and white" mode; do something..
}{
% "color" mode; do something different..
}

Now you can call LaTeX like this:

latex '\newcommand{\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.

[edit] Creating your own package

If you define a lot of new environments and commands, the preamble of your document will get quite long. In this situation, it is a good idea to create a LaTeX package containing all your command and environment definitions. You can then use the \usepackage command to make the package available in your document. Writing a package basically consists of copying the contents of your document preamble into a separate file with a name ending in .sty.

It is very simple, just follow the steps:

  1. create a simple text file called mypack.sty (or any other name you like) and open it with any text editor
  2. at the very beginning of the text document just write
    \ProvidesPackage{mypack}
    
    note: it has to have the same name of the file without the extension. It tells LaTeX the name of the package and will allow it to issue a sensible error message when you try to include a package twice.
  3. write whatever you want in it using all the LaTeX commands you know. Normally you should define new commands or import other packages.
  4. import your new package with the known command
    \usepackage{mypack}
    
  5. or
    \RequirePackage{mypack}
    

the file mypack.sty and the LaTeX source you are compiling must be in the same directory. It will be as though all you have written within your package is within the document itself.

Alternatively, it is possible to place the package within ~/texmf/tex/latex/mypack/mypack.sty where '~' is your home directory (On Windows this is often C:\Users\username) and where mypack is the name of your package. Running texhash or equivalent will allow you to use your package as detailed above, but without it needing to be in the same directory as your document.

[edit] Creating your own style

It is also possible to create your own style file. The process is 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 line has to be provided:

\ProvidesClass{mystyle}

again, within the style files other files or packages are imported by the requirepackage command.


Previous: General Guidelines Index Next: Packages
Personal tools
Namespaces
Variants
Actions
Navigation
Community
Toolbox
Sister projects
Print/export