2

I am trying to define a macro which I will include in a class definition. For the time being I put the macro definition in the normal document. Compiling the following document fails.

\documentclass[10pt,a4paper]{article}

\makeatletter
\def\ntcstitle#1{\def{\@ntcstitle}{#1}}
\newcommand{\thentcstitle}{\@ntcstitle}
\makeatother

\ntcstitle{Lorem Ipsum}

\begin{document}
\thentcstitle\par
\end{document}

This example is something I copied from another question here, which seemed to work:

Define variable in a class

I also tried to move this definition into a class file, omitted the \makeatletter and \makeatother commands. Using the class file was successful, the error message is the same.

This is the error message:

! Missing control sequence inserted.
 <inserted text> 
                \inaccessible 
 l.10 \ntcstitle{Lorem Ipsum}

Then I replaced the macro with the LaTeX style definition:

\documentclass[10pt,a4paper]{article}
\usepackage{lipsum}

\makeatletter
\newcommand{\ntcstitle}[1]{\newcommand{\@ntcstitle}{#1}}
\newcommand{\thentcstitle}{\@ntcstitle}
\makeatother

\ntcstitle{Lorem Ipsum}

\begin{document}
\thentcstitle\par
\end{document}

And that works as expected.

I tried to use \def instead of \newcommand because I assumed \def was the correct way to define macros in class files. What I am doing wrong here?

  • Use \newcommand if you can as it is safer. What is the purpose of the command exactly? I'm not sure why you are defining 3 different commands. – cfr Dec 1 '14 at 0:43
  • Although I admit to writing it, you might find this answer helpful. \newcommand is LaTeX. \def is TeX. Only use \def when you know why \newcommand isn't suitable. – cfr Dec 1 '14 at 0:45
  • Your code seems to be equivalent to \newcommand{\ntcstitle}[1]{\newcommand{\thentcstitle}{#1}}? – cfr Dec 1 '14 at 0:49
  • @cfr: The intention of the command is to define \@ntcstitle. Later I use the macro \thentcstitle to print the variable \@ntcstitle. I define the macro to set \@ntcstitle only once, but two different MWE. – jlinkels Dec 1 '14 at 1:08
  • @cfr: If \newcommand is the preferred way, I'll use it. – jlinkels Dec 1 '14 at 1:13
5

You should not use \def in latex but if you do the syntax is

   \def\@ntcstitle{#1}}

not

   \def{\@ntcstitle}{#1}}

(The #1 and the trailing } of course only valid in the context of your definition of \ntcstitle)

  • @paul The extra brace was intentional, I'll add a note in the answer – David Carlisle Dec 1 '14 at 0:48
  • 1
    That is a stupid mistake. I have looked for 2 days at this code and the example and I did not notice the {}. Aaarrggghhhh – jlinkels Dec 1 '14 at 1:05
  • @DavidCarlisle Sorry for my miscorrection: I did not read carefully the question... – Paul Gaborit Dec 1 '14 at 7:38
  • @jlinkels: I strongly feel with you! Well, you should admit yourself that the error message was not exactly helpful to find the culprit. – Daniel Dec 19 '14 at 14:40

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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