2

I achived generating smaller macrons thanks to Tobi's code from Question 263549, but Latex throws "Undefined control sequence" if I try to use it in \section, \subsection and so on. How can I make the command \smartmacron (code follows) work in headings?

\newlength\tmp
\newcommand{\smartmacron}[1]{%
   \settowidth{\tmp}{#1}%
   \makebox[\tmp][c]{%
      \rule[1.2ex]{0.6\tmp}{0.035em}%
   }\kern-\tmp#1%
}

Mnot-WE:

\documentclass{scrartcl}
\newlength\tmp
\newcommand{\smartmacron}[1]{%
   \settowidth{\tmp}{#1}%
   \makebox[\tmp][c]{%
      \rule[1.2ex]{0.6\tmp}{0.05em}%
   }\kern-\tmp#1%
}
\begin{document}
  \section{\smartmacron e}
\end{document}
  • 1
    I don't get the "undefined control sequence" error with your mwe, but I can see a potential problem with the fragile definition of \smartmacron which would need protecting (or define robustly). For example \section{\protect\smartmacron e}. – Nicola Talbot Dec 11 '15 at 19:09
  • @NicolaTalbot Thanks, this helped a lot and already solved the problem! Can you provide any link on how to define it robustly, as you suggested? I don't really know what this means… – dessert Dec 11 '15 at 20:20
2

The LaTeX kernel provides \DeclareRobustCommand which can be used to define a robust command. If you use this you don't need to protect the command in a moving argument (such as the argument to \section).

MWE:

\documentclass{scrartcl}
\newlength\tmp
\DeclareRobustCommand{\smartmacron}[1]{%
   \settowidth{\tmp}{#1}%
   \makebox[\tmp][c]{%
      \rule[1.2ex]{0.6\tmp}{0.05em}%
   }\kern-\tmp#1%
}
\begin{document}
  \tableofcontents
  \section{\smartmacron e}
\end{document}

The etoolbox package provides \newrobustcmd as an alternative to \DeclareRobustCommand that issues an error (rather than an easily-missed information message) if the command is already defined, which is safer:

\newrobustcmd{\smartmacron}[1]{%
   \settowidth{\tmp}{#1}%
   \makebox[\tmp][c]{%
      \rule[1.2ex]{0.6\tmp}{0.05em}%
   }\kern-\tmp#1%
}
  • Thank you very much! Defining \let\=\smartmacron one can overwrite the ordinary macron with this adaptive version, so that the usual \= calls the new defined function. – dessert Dec 13 '15 at 9:58

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.