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.

Just noticed a somewhat strange behavior, when trying to show internal Latex @ commands from main document via \newcommand.

Consider the following MWE, where all instances of the internal macro ( \c@something) calls are already wrapped in \makeatletter/\makeatother:

\documentclass[10pt]{article}


% define internal at-macro
\makeatletter
\def\c@something{AAA}
\typeout{ == \c@something == }
\makeatother

% define a newcommand calling the at-macro
\newcommand{\mytest}{
\makeatletter
\c@something
\makeatother
}



\begin{document}


test

\mytest{}

\makeatletter
here \c@something
\makeatother


\end{document}

When I build the above with pdflatex, I get this:

test.png

... however, I would have expected something like:

test
AAA
here AAA

Can anyone explain why does the macro not get called/executed when put as part of \newcommand - and else looks ok called directly from the main body? (In other words, could I get its output as part of \newcommand at all?)

share|improve this question

1 Answer 1

up vote 11 down vote accepted

TeX converts characters to tokens when it first reads them. So when you do

\newcommand{\mytest}{
\makeatletter
\c@something
\makeatother
}

TeX 'sees' @ as an 'other' character, and the definition of \mytest is:

  1. A space (as you have a new line with no % just after the opening {)
  2. The token \makeatletter
  3. The token \c
  4. The 'other' character @
  5. The 'letter' tokens 'something' (each separate)
  6. The token \makeatother

What you wanted was

\makeatletter
\newcommand{\mytest}{%
  \c@somecommand
}
\makeatother

in which case the definition of \mytest is the single token \c@somecommand, exactly as for the \def case.

share|improve this answer
    
Awesome - many, many thanks for that, @JosephWright! Special thanks for breaking it down character by character - it also cleared up other things for me (such as the comment before the end of line thing :)) ; cheers! –  sdaau May 14 '12 at 19:46

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.