# \emph and punctuation

I know \emph is to be used to emphasize text. However once in a while, a punctuation mark follows emphasized text, mostly commas and periods. When \emph makes text italic, it should extend to the punctuation mark, but other emphasis like underlining, color, background, etc. should not.

Can I have a solution in the preamble that defines \emph such that it extends to punctuation marks which follow the text?

I.e. I want

This is a \emph{small}, simple example.


to be rendered as

This is a small, simple example.

and not to become

This is a small, simple example.

• This is a \emph{small,} simple example.? – CarLaTeX Jan 3 '17 at 21:30
• @CarLaTeX I think the OP would like a generic way to redefine \emph{} such that it emphasises text by means of italicisation and, say, colouring it red, but, if say the emphasised text is followed by punctuation, the punctuation should also be italicised, but not coloured red. So, \emph{small,} could be used to make small red and italic and the comma italic, but not red. But I think the OP wants generic solutions that could include many types of emphasis, but only the italicisation would extend to punctuation – Au101 Jan 3 '17 at 21:36
• @Au101 I haven't read with the due attention, sorry! – CarLaTeX Jan 3 '17 at 21:40
• @CarLaTeX It's alright, actually perhaps the OP could clarify a little bit? I was confused too. Re-reading it, seems that the OP is using \emph{} for italicisation, and then, say, nesting other emphasis, so like \emph{\textcolor{red}{small}}, and it seems like they would like the \emph alone to extend to any punctuation outside the braces, but nothing else, which it seems is how they would like to try to achieve their desired result. I'm not sure that would be the best approach though – Au101 Jan 3 '17 at 21:43

\documentclass{article}

\usepackage{xspace}
\let\Emph\emph
\makeatletter
\def\emph#1{%
\@ifnextchar,{\@emph@i#1}{%
\@ifnextchar;{\@emph@ii#1}{%
\@ifnextchar.{\@emph@iii#1}{%
\@ifnextchar!{\@emph@iv#1}{\Emph{#1}\xspace}}}}}
\def\@emph@i#1,{\Emph{#1,}\xspace}
\def\@emph@ii#1;{\Emph{#1;}\xspace}
\def\@emph@iii#1.{\Emph{#1.}\xspace}
\def\@emph@iv#1!{\Emph{#1!}\xspace}
\makeatother
\begin{document}

This is a \emph{small}, simple example.\par
This is a \emph{small,} simple example.\par
This is a \emph{small}; simple example.\par
This is a \emph{small;} simple example.\par
This is a \emph{small}. simple example.\par
This is a \emph{small.} simple example.\par
This is a \emph{small}! simple example.\par
This is a \emph{small!} simple example.

\end{document}


• The answer is great and helpful, but only solves the problem partly. I tried to expand the solution to consider other punctuation as well, but \def\@emph#1,{...} seems not to work with more than one punctuation mark. – Bolpat Jan 6 '17 at 20:14
• it is chain of \@ifnextchar,{..}{\@ifnextchar.{..}{\@ifnextchar;{}{}}}} – user2478 Jan 6 '17 at 20:38
• I tried that. The problem is extending \def\@emph#1,{\Emph{#1,}\xspace}. I couldn't figure out how to that. Something like overloading didn't work; I tried \def\@emph#1,{\Emph{#1,}\xspace} \def\@emph#1.{\Emph{#1.}\xspace}. I also tried \def\@emph#1#2{\Emph{#1#2}\xspace}, but that matches just two letters. I suspect I cannot restrict #2 to match punctuation? – Bolpat Jan 8 '17 at 4:06
• see edited answer. Can easily be extended – user2478 Jan 8 '17 at 5:52
• Usual caveat: doing \let\x\y when \y has been defined with \DeclareRobustCommand (like \emph) is dangerous and \LetLtxMacro should be employed; see When to use \LetLtxMacro – egreg Jan 8 '17 at 9:11