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 was able to use my scalerel package to hopefully give you what you want (scalerel was just sent to CTAN today, so until it propagates, you can find the style listing at How to horizontally merge two symbols?).
Using that package's features, my first cut was to create a command \myabs, which places abs bars around anything. It will stretch the size of the bars exactly to the argument. The downside of this first attempt is that you may have preferred the bars to extend a bit above and below the object being surrounded (addressed later in this answer). Here's the code for the first approach:
\documentclass{article}
\usepackage{scalerel}
\usepackage{mathtools}
\begin{document}
% [METHOD USED IN ORIGINAL QUESTION CODE HERE, REMOVED FROM THIS LISTING]
\newcommand\myabs[1]{%
\setbox1\hbox{$#1$}%
\stretchrel{\lvert}{\usebox1}\stretchrel*{\lvert}{\usebox1}%
}
\[ \myabs{\vec{k}}~~\myabs{\vec{x}}~~\myabs{\vec{A}}~~\myabs{\vec{q}} \]
\end{document}
and here's the output, as compared to your original
However, to answer ralfix's request to extend the vertical line a bit above and below the surrounding object, I just used the \addvbuffer routine from the verbatimbox package, setting the top and bottom add-on to 2pt:
\documentclass{article}
\usepackage{scalerel}
\usepackage{mathtools}
\usepackage{verbatimbox}
\setlength\boxtopsep{2pt}
\setlength\boxbottomsep{2pt}
\newcommand\myabs[1]{%
\setbox1\hbox{$#1$}%
\setbox2\hbox{\addvbuffer{\usebox1}}%
\stretchrel{\lvert}{\usebox2}\stretchrel*{\lvert}{\usebox2}%
}
\begin{document}
\[ \myabs{\vec{k}}~~\myabs{\vec{x}}~~\myabs{\vec{A}}~~\myabs{\vec{q}} \]
\end{document}
But egreg commented that the height of the abs bar should not change with the argument. If that is the preferred embodiment of abs, then scalerel can fix that too:
\documentclass{article}
\usepackage{scalerel}
\def\lvert{|}
\begin{document}
METHOD 2: FIXED EXTENT
\newsavebox\mybox
\savebox{\mybox}{$\stretchrel*{|}{\rule[-.6ex]{0ex}{3ex}}$}
\def\myabs#1{\usebox\mybox#1\usebox\mybox}
\[ \myabs{\vec{k}}~~\myabs{\vec{x}}~~\myabs{\vec{A}}~~\myabs{\vec{q}} \]
\end{document}
Best Answer
Using "all italics" is unfortunately an often committed sin. You should italicize only variables.
Everything else should be upright. For example:
Exceptions to this rule may still apply, i.e. the Euler-number $e$ is no variable, but still traditionally written in italics.
Some of the above was stolen from the "Please Make A Note" blog. More detailed information can be found in "Typefaces" from NIST.