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 01 '14 at 00: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 01 '14 at 00:45
  • Your code seems to be equivalent to \newcommand{\ntcstitle}[1]{\newcommand{\thentcstitle}{#1}}? – cfr Dec 01 '14 at 00: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. – Johannes Linkels Dec 01 '14 at 01:08
  • @cfr: If \newcommand is the preferred way, I'll use it. – Johannes Linkels Dec 01 '14 at 01:13

1 Answers1

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)

David Carlisle
  • 757,742