4

I want to declare a macro \myemph which typesets its argument in italics. When \emph is used in \myemph, it should use boldface. But when \emph is used outside of \myemph, it should still use italics:

\emph{italic, \emph{upright}} % as usual
\myemph{italic, \emph{bold and italic}} % new command

(For the moment, I do not care about nested \emph in \myemph.)

I have attempted to declare the macro below. This seems to work fine. However, I'm getting weird errors when I use this in section or chapter titles:

\documentclass{article}

\makeatletter
\NewCommandCopy\org@emph\emph
\DeclareTextFontCommand{\bfemph}{\bfseries}
\newcommand{\myemph}[1]{\textit{\RenewCommandCopy\emph\bfemph#1\RenewCommandCopy\emph\org@emph}}
\makeatother

\begin{document}

\myemph{italic, \emph{bold and italic}} % works

\section{\myemph{Section}} % error

\end{document}

(I'm not sure if there's any benefit to defining \bfemph over just using \textbf.)

The error I get is: Argument of \@sect has an extra }. How can I define \myemph so that it also works in this environment?

1 Answer 1

6

You can change what \emph does by using \DeclareEmphSequence{<font change command list>}. You can restrict that to your \myemph command by enclosing that in a group:

enter image description here

\documentclass{article}

\DeclareRobustCommand{\myemph}[1]{%
  \begingroup
    \DeclareEmphSequence{\itshape,\bfseries}%
    \emph{#1}%
  \endgroup}

\begin{document}

\myemph{italic, \emph{bold and italic}} % works

\section{\myemph{Section}} % error

\end{document}
1
  • 2
    @Keelan Usually when a nontrivial macro is used in section titles or figure captions and it breaks, the solution is to define that macro with \DeclareRobustCommand (I wrote something about that earlier today here). Your \myemph macro is not expandable, but writing to the .toc file forces it to expand and it breaks. Making it robust solves the problem Oct 22, 2021 at 14:18

You must log in to answer this question.

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