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?

4
  • \csname @#1\endcsname Commented Apr 20, 2018 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? Commented Apr 20, 2018 at 23:40
  • Why not using \makeatletter\newcommand{\warning}[1]{\@ifundefined{@#1}{Booo!!!}{Yay!!!}}\makeatother? That's what \@ifundefined is made for... Commented Apr 21, 2018 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. Commented Apr 21, 2018 at 6:30

2 Answers 2

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} 

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.