11

The \emph command is smart. It behaves differently depending of the nesting level.

\emph{Italics within \emph{italics within \emph{more italics} and} so on}.

outputs:

Italics within italics within more italics and so on.

How do I redefine the \emph command to behave as:

  • first level: 'single quotes',
  • second level: SMALL-CAPS,
  • third level: italics?
7
\documentclass{article}
\newcounter{emphlevel}
\renewcommand\emph[1]{\stepcounter{emphlevel}%
  \ifnum\value{emphlevel}=1`#1'\else
    \ifnum\value{emphlevel}=2\textsc{#1}\else
    \ifnum\value{emphlevel}=3\textit{#1}\else
  \fi\fi\fi\addtocounter{emphlevel}{-1}%
}
\begin{document}
\emph{Single quotes within \emph{Small Caps within \emph{more italics} and} so on}.
\end{document}

enter image description here

The above ORIGINAL answer truncates levels beyond the third. This below variation repeats the cycles repeatedly through the specified emphases.

\documentclass{article}
\newcounter{emphlevel}
\renewcommand\emph[1]{\stepcounter{emphlevel}%
  \ifnum\value{emphlevel}=1\textup{`#1'}\else
    \ifnum\value{emphlevel}=2\textsc{#1}\else
    \ifnum\value{emphlevel}=3\textit{#1}\else
      \addtocounter{emphlevel}{-4}\emph{#1}\addtocounter{emphlevel}{4}%
  \fi\fi\fi\addtocounter{emphlevel}{-1}%
}
\begin{document}
\emph{Single quotes within \emph{Small Caps within \emph{more italics
  within \emph{Single quotes within \emph{Small Caps within \emph{more italics} 
  and} so on}} and} so on}.

\emph{Aa \emph{Bb \emph{Cc \emph{Dd \emph{Ee \emph{Ff \emph{Gg}}}}}}}
\end{document}

enter image description here

7

A simple application of grouping:

\documentclass{article}

\newcount\emphlevel
\DeclareRobustCommand{\emph}[1]{%
  \begingroup
  \normalfont
  \advance\emphlevel by 1
  \ifcase\emphlevel
  \or
    `\aftergroup'\normalfont
  \or
    \normalfont\expandafter\textsc
  \or
    \normalfont\expandafter\textit
  \else
    \normalfont!!!!\expandafter\textbf
  \fi
  {#1}%
  \endgroup
}

\begin{document}

Normal text.
\emph{Single quotes within \emph{Small Caps within \emph{more italics} and} so on}.
Again normal text.

\emph{Single quotes within \emph{Small Caps within \emph{more italics}}}.

\end{document}

enter image description here

  • 1
    This makes my brain hurt when I try to understand it, but I tried. The emphlevel counter gets advanced each time there is another emph command, and the value gets fed to ifcase 0 normal, 1 smallcaps, 2 italics, 3 boldface. So why aftergroup, normalfont and normalfont!!!! ? – A Feldman May 11 '16 at 2:33
  • @AFeldman \aftergroup is for adding the trailing quote at the first level; \normalfont is just for safety. The !!!!! are for visually showing that you're gone too deep. – egreg May 11 '16 at 6:00
4

I decided to use another command name, but the nesting can be controlled with a counter and \ifcase...\fi conditional.

The 4th level will provide an usual \@ctrerr, but this could be shifted to basically any level.

\documentclass{article}


\newcounter{smartlevel}

\makeatletter
\newcommand{\smartcmd}[1]{%
  \stepcounter{smartlevel}%
  \ifcase\c@smartlevel
  \or'#1'\addtocounter{smartlevel}{-1}%
  \or\textsc{#1}\addtocounter{smartlevel}{-1}%
  \or\textit{#1}\addtocounter{smartlevel}{-1}%
  \else
  \@ctrerr% Too deeply nested
  \fi
}
\makeatother

\begin{document}

\smartcmd{First \smartcmd{Foo \smartcmd{inner} \smartcmd{inner again}}}

\smartcmd{First \smartcmd{Foo} \smartcmd{Foo level \smartcmd{inner again}}} 

\end{document}

enter image description here

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.