Upright parentheses in italic text
A LaTeX3 solution. I chose to simply do what you said, replacing every (
by \textup{(}
, every )
with \textup{)}
, and similarly for brackets, prior to passing the result to the old version of \emph
for typesetting.
At the end of the day, xparse
allows us to easily define \emph
to do what you asked for, and \emph*
to do the old version of \emph
.
\documentclass{article}
\usepackage{expl3,xparse}
\ExplSyntaxOn
\cs_new_eq:Nc \emph_old:n { emph~ } % Copying the old definition of `\emph`
\cs_new_protected:Npn \emph_braces:n #1 % Set up how braces should be typeset.
{ \mode_if_math:TF {#1} { \textup{#1} } }
\cs_new:Npn \emph_new:n #1 {
\tl_set:Nn \l_emph_tl {#1}
\tl_replace_all:Nnn \l_emph_tl {(}{\emph_braces:n{(}}
\tl_replace_all:Nnn \l_emph_tl {)}{\emph_braces:n{)}}
\tl_replace_all:Nnn \l_emph_tl {[}{\emph_braces:n{[}}
\tl_replace_all:Nnn \l_emph_tl {]}{\emph_braces:n{]}}
\exp_args:NV \emph_old:n \l_emph_tl
}
\RenewDocumentCommand {\emph} {sm} {
\IfBooleanTF {#1} {\emph_old:n {#2}} {\emph_new:n {#2}}
}
\ExplSyntaxOff
\begin{document}
A \emph{(simple) example}, and \emph*{another one (with no correction)}.
Also some math \emph{\((x+y)^2\) and text (again)}.
\end{document}
EDIT: Alan Munn pointed out a mistake in a comment. Thanks.
EDIT2: An update to expl3
renamed \tl_replace_all_in:Nnn
to \tl_replace_all:Nnn
.
EDIT3: Barbara Beeton pointed out that I should be using \textup
rather than \emph
to set parentheses upright. She also mentioned that "it was suggested at one time to create a 'theorem font' in which the alphabet is italic and fences (parens, brackets, braces) are upright", but this font was never made. See comments below.
EDIT4: I had been sloppy when copying the definition of \emph
, and this got revealed by the latest xparse
update. LaTeX2e's \emph
(like many other commands) uses \emph
to hold the real code of \emph
(note the trailing space). The line below \ExplSyntaxOn
was thus changed to \cs_new_eq:Nc
... { emph~ }
: the "c" argument specifier turns emph~
into the appropriate command \emph
.
EDIT5: I've added code to avoid changing braces in math mode. Or rather the command used to typeset braces is now math aware (this is much simpler than trying to detect math when doing the replacement).
I would agree not messing with catcodes (although doing so just within an \emph
command itself probably wouldn't have too many issues.) But here's a relatively simple solution with a slightly more transparent semantics than Yiannis's solution:
\documentclass{article}
\newcommand{\bemph}[1]{{\upshape#1}} % define how emphasised brackets should look
\newcommand{\ep}[1]{\bemph{(}#1\bemph{)}} % parentheses
\newcommand{\eb}[1]{\bemph{[}#1\bemph{]}} % square brackets
\begin{document}
This is \emph{a \ep{simple} example}.
\emph{This is \emph{a \ep{simple} example}.} % also works embedded in another emph
\end{document}
The advantage of doing things this way is that if you ever need to change what \emph
does, you have independent control over what emphasised brackets are. For example if you redefine \emph
as underlining or \textbf
you can adjust the brackets accordingly.
I would keep it simple and not mess up with catcodes and the like. I would define a small macro as follows (you can use a shorter name if you like).
\documentclass{article}
\usepackage{xspace}
\def\bracketemphasis#1#2{(\emph{#1})\xspace\emph{#2}\xspace}
\begin{document}
\bracketemphasis{simple}{example}.
\end{document}
The xspace
would correctly handle spacing after punctuation and that is why I included it.
\xspace
should be used at the end of a macro designed to be used mainly
in text. It adds a space unless the macro is followed by punctuation
characters.