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}
2
  • 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}. Dec 11, 2015 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, 2015 at 20:20

1 Answer 1

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%
}
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, 2015 at 9:58

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .