Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

Looking for a way to customise a table of contents, I came across this recommendation to use tocloft. As per the suggestion (and the documentation), I've put a conditional in place which tests for the presence of subfig and loads the package appropriately.

This Minimum [non-]Working Example:

\documentclass{article}
\@ifpackageloaded{subfig}
    {\usepackage[titles,subfigure]{tocloft}}
    {\usepackage[titles]{tocloft}}

\begin{document}
    \tableofcontents
    \section{Section 1}
    Hello 1
    \section{Section 2}
    Hello 2
    \section{Section 3}
\end{document}

Produces several errors including:

./tocloft.tex:3: You can't use `\spacefactor' in vertical mode.
\@->\spacefactor 
Removing the call to `\@ifpackageloaded` and explicitly specifying 

and

Missing character: There is no è in font cmex10!
/usr/local/texlive/2011/texmf-dist/tex/latex/tocloft/tocloft.sty:35: You can't 
use `\spacefactor' in vertical mode.
\@->\spacefactor 

Looking around for a solution, the TeX FAQ suggests this is the result of the package using "... an internal command without taking 'precautions'". Explicitly calling either \usepackage[titles,subfigure]{tocloft} or \usepackage[titles]{tocloft} works as advertised and is how I'm currently working around the problem. I would, however, like to implement the conditional loading as a safety precaution.

In an effort to learn more about (La)TeX internals, I'd like to know what's the root cause of this error. Is this a problem specific to my (MacTeX 2011) installation, or can others reproduce it? Also, what can be done to fix it in this particular incidence? Maybe the package's current maintainer could provide some insight into this.

share|improve this question
    
I've edited the question you link to so that others don't fall into the same trap that you did. –  Alan Munn Sep 28 '11 at 4:10
add comment

1 Answer

up vote 3 down vote accepted

The problem here is a common one. The use of @ is restricted to style files. If you want to use it in your document preamble (or elsewhere), you need to encompass it using the

\makeatletter
...
\makeatother

pair. So, in your preamble, use

\makeatletter
\@ifpackageloaded{subfig}
    {\usepackage[titles,subfigure]{tocloft}}
    {\usepackage[titles]{tocloft}}
\makeatother

Read the entries \@ and @ in macro names and \spacefactor complaints on the UK TeX FAQ, as well as the discussion What do \makeatletter and \makeatother do?

share|improve this answer
    
After making my post, I followed the "\@ and @ in macro names" link and was just reading through it. Thanks for clarifying this. –  coffeebucket Sep 28 '11 at 3:28
    
@Wermer: You want to add a link to What do \makeatletter and \makeatother do? in your answer, and then I can delete my answer. –  Peter Grill Sep 28 '11 at 4:37
    
@PeterGrill: Thanks - just did that. –  Werner Sep 28 '11 at 4:51
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.