3

Can i make a macro in a class file that take a string like argument and call a local macro? I want to make the following:

\newcommand{\somename}{\def\@somename{#1}}

\newcommand{\warning}[1]
    { \ifthenelse
          {\isundefined{\@#1}}
          {You have to define #1. Use this macro: \textbackslash#1}
          {}
    }
\AtBeginDocument {\warning{somename}}

Then, if in the document file i didnt define \@somename i will have a warning saying that to me.

Any ideas?

  • \csname @#1\endcsname – Manuel Apr 20 '18 at 22:49
  • 2
    Welcome! Are we in a package or class file? What should the definition of the new macro be? Please provide a complete minimal working example (code for a small document) which shows us how you'd like to use these macros - how will \@date then be used, for example? – cfr Apr 20 '18 at 23:40
  • Why not using \makeatletter\newcommand{\warning}[1]{\@ifundefined{@#1}{Booo!!!}{Yay!!!}}\makeatother? That's what \@ifundefined is made for... – user31729 Apr 21 '18 at 5:07
  • Define/initialize \@somename as the warning and do the redefinition with \somename to have the real string instead of the former warning. – Johannes_B Apr 21 '18 at 6:30
3

The LaTeX kernel already provides this:

\makeatletter % not in a class or package file
\newcommand{\warning}[1]{%
  \@ifundefined{@#1}
    {You have to define '#1'. Use this macro: \texttt{\symbol{`\\}#1}}
    {}%
}

\newcommand{\somename}[1]{\def\@somename{#1}}
\makeatother % not in a class or package file

\AtBeginDocument{\warning{somename}}

Note that the first argument to \@ifundefined should be a string of characters representing the macro name (no initial backslash).

0

I would use the etoolbox pacakge:

enter image description here

Code:

\documentclass{article}
\usepackage{etoolbox}

\newcommand*{\DefinePerson}[1]{\csdef{#1}{}}
\newcommand*{\Warning}[1]{%
    \ifcsdef{#1}{}{%
        You have to define #1. Use the macro {\ttfamily\string\DefinePerson}.%
    }%
}%

\DefinePerson{Steve Jobs}
\DefinePerson{Bill Gates}

\AtBeginDocument{%
    \Warning{Steve Jobs}%
    \Warning{Warren Buffet}%
}%

\begin{document}
\par
Hi there
\end{document}

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.