LaTeX tips and tricks

http://myitcv.org.uk / latex /

Questions

  1. How to include the LaTeX and BibTeX logos in your document
  2. How do I cite an author's name (from a BibTeX entry) in my document without re-typing his/her name?
  3. How do I ask Vim to convert the current window of syntax highlighted LaTeX into HTML suitable for displaying on my web pages?
  4. How do I write 'smart' macros that give one output inside math mode and another in normal mode?
  5. How do correct spacing around custom macros in math mode?
  6. How do I handle long equations? Can I split them onto two lines?
  7. How do I change the format of captions for figures? Can I change the word 'Figure'?
  8. Can I create figures within figures (or sub-figures) labelled 'Figure 1(a)' etc?
  9. How do I modify the LaTeX search path to include directories that contain my own .{cls,sty} files?


Answers

  1. How to include the LaTeX and BibTeX logos in your document
    
    
      \documentclass{article}
    
      % This is where the logos are to be found
      \usepackage{doc}
      \begin{document}
    
      The LaTeX logo: \LaTeX
    
      The BibTeX logo: \BibTeX
    
      \end{document}
    
      
  2. How do I cite an author's name (from a BibTeX entry) in my document without re-typing his/her name?

    I use the natbib package. This uses ordinary .bib input, but provides a variety of output macros:

    
      % This file is test.tex. test.bib also exists in this directory
    
      \documentclass{article}
    
      \usepackage{natbib}
    
      % There are two variations on how natbib can be used
      %
      % Have items enumerated in your bibliography
      % \usepackage[numbers]{natbib}
      %
      % Author year format (default)
      % \usepackage[authoryear]{natbib}
    
      \begin{document}
    
      \citeauthor{jolly04example} explains how to include bibliography material
      into using natbib. See \citep{jolly04example} for more examples.
    
      % The natbib package supplies three style files that replace the standard
      % abbrv.bst, plain.bst and unsrt.bst: abbrvnat.bst, plainnat.bst and unsrtnat.bst.
      % Each supports authoryear and numerical citations.
      % 
      \bibliographystyle{abbrvnat}
      \bibliography{test}
    
      \end{document}
    
      

    The complete list of \cite* macros can be found in the package readme file.

  3. How do I ask Vim to convert the current window of syntax highlighted LaTeX into HTML suitable for displaying on my web pages?

    The command from within Vim is :TOhtml. See :help convert-to-HTML for example invocations.

    Not only can Vim output HTML suitable for older browsers, it can also generate CSS style code.

  4. How do I write 'smart' macros that give one output inside math mode and another in normal mode?

    Use \ifmmode:

    
      \documentclass{article}
    
      % In the example below
      % \test nested inside an array (inside math mode)
      % would still give \ifmmode as true; our intuition
      % demands this.
      \def\test{\leavevmode\ifmmode math \else normal \fi}
    
      \begin{document}
    
      \test
      $\test$
    
      \end{document}
    
      
  5. How do correct spacing around custom macros in math mode?

    Use \math* macros (see page 206 of the LaTeX companion):

    
      \documentclass{article}
    
      \begin{document}
    
      \begin{math}
        \begin{array}{l}
    
          a trial b \\
    
          % trial can be arbitrarily complex
          a \mathbin{trial} b
    
        \end{array}
      \end{math}
    
      \end{document}
    
      
  6. How do I handle long equations? Can I split them onto two lines?

    Use the split environment provided by amsmath:

    
      \documentclass{article}
    
      %
      % amsmath provides split
      %
      \usepackage{amsmath}
    
      % macros to reproduce a long equation --- specific to my work
      \newcommand{\code}[1]{{\tt #1}{}}
      \newcommand{\ObjType}[4]{\ensuremath{ObjectType\{#1_{#3}:#2_{#4}\}_{1 \leq #3 \leq #4}}}
      \newcommand{\eif}{\mbox{\bf~~if~~}}
      \newcommand{\eand}{\mbox{\bf~~and~~}}
      \newcommand{\SubType}[2]{\ensuremath{#1~\mbox{\code{<:}}~#2}}
    
      \begin{document}
    
      \begin{equation}
        \begin{split}
          \SubType{\ObjType{m}{S}{j}{m}}{\ObjType{m}{T}{i}{n}} \\ % split here
              \eif n \leq m \eand \mbox{for each~} i \leq n, \SubType{S_i}{T_i}
        \end{split}
      \end{equation}
    
    
      \end{document}
    
      
  7. How do I change the format of captions for figures? Can I change the word 'Figure'?

    Use ccaption:

    
      \documentclass{article}
    
      % ccpation provides this capability
      \usepackage{ccaption}
    
      % Change the format of a figure caption
      % For more options see the package documentation
      \captionnamefont{\bfseries}
      \captiontitlefont{\small\sffamily}
      \captiondelim{ --- }
      \hangcaption
      \renewcommand{\figurename}{Fig.}
    
      \begin{document}
    
      \begin{figure}
        Test
      \caption{This is a test}
      \end{figure}
    
      \end{document}
    
      

    For more details, see the ccaption package. Follow the installation instructions at the bottom of the page.

  8. Can I create figures within figures (or sub-figures) labelled 'Figure 1(a)' etc?

    Use subfigure:

    
      \documentclass{article}
    
      % subfigure provides this functionality
      \usepackage{subfigure}
    
      \begin{document}
    
      \begin{figure}
        \subfigure[Caption for first subfigure]
        {
          \label{FirstSubFigure}\fbox{Contents of first subfigure}
        }
        \subfigure[Caption for second subfigure]
        {
          \label{SecondSubFigure}\fbox{Contents of second subfigure}
        }
      \caption{Caption for the overall figure}
      \label{OverallFigure}
      \end{figure}
    
      Reference the overall figure: \ref{OverallFigure}.
    
      Reference the first subfigure: \ref{FirstSubFigure}.
    
      Reference the second subfigure: \ref{SecondSubFigure}.
    
      \end{document}
    
      

    For more details, see the subfigure package. Download all the listed files and type make to extract the package and related material.

  9. How do I modify the LaTeX search path to include directories that contain my own .{cls,sty} files?

    Under Linux/UNIX, set the TEXINPUTS environment variable. TEXINPUTS is the search path for resource files including graphics, packages and document classes.

    
      # ** IMPORTANT **
      #
      # The trailing colon indicates the standard search
      # path should be appended to the user specified
      # TEXINPUT variable
    
      # Paths are ':' separated
      export TEXINPUTS=".:~/path/to/add:"
      export TEXINPUTS=".:~/path/to/add:/second/path/to/add:"
    
      # Trailing '/' for directories is optional
      export TEXINPUTS=".:~/path/to/add/:"
    
      # double trailing slash indicates this directory is
      # to be search recursively
      export TEXINPUTS=".:~/path/to/add//:"
    
      

    Note: the commands shown are for setting environment variables within the Bash shell. Use setenv (check invocation in man pages) for the Tcsh shell.