How to get upright parentheses in the whole document?
As egreg warned, the following will probably break many things. But it works in simple situations. [code edited following egreg's advice]
[update: at the bottom of this answer I edit the answer to be compatible with \label
and \ref
]
\documentclass{article}
\newtheorem{theorem}{Theorem}
\catcode1=12
\catcode2=12
\mathcode1=\the\mathcode`\(
\delcode1=\the\delcode`\(
\mathcode2=\the\mathcode`\)
\delcode2=\the\delcode`\)
\catcode`\(=\active
\catcode`\)=\active
\everymath\expandafter{\the\everymath\let(^^A\let)^^B}
\everydisplay\expandafter{\the\everydisplay\let(^^A\let)^^B}
%%\def({\begingroup\upshape\char`\(\endgroup}
%%\def){\begingroup\upshape\char`\)\endgroup}
\def({\textup{\char`\(}}
\def){\textup{\char`\)}}
\begin{document}
\begin{theorem}
I want parenthesis (around this text) to be upright. Of course, parentheses in
math mode are already upright: $\Bigg((E = mc^2)\Bigg)$, and we don't want to
fiddle with them.
\end{theorem}
\[ \Bigg((E = mc^2)\Bigg) \]
I want parenthesis (around this text) to be upright.
\emph{I want parenthesis (around this text) to be upright. }
\end{document}
with egreg's suggestion the spacing appears to be better:
Code for (hopefully...) compatibility with \label
and \ref
(with or without hyperref
used in the document):
\documentclass{article}
\usepackage{hyperref}
\newtheorem{theorem}{Theorem}
\def\makeparenletter{\catcode`\(=11 \catcode`\)=11 }
\def\makeparenother{\catcode`\(=12 \catcode`\)=12 }
\def\makeparenactive{\catcode`\(=\active\catcode`\)=\active}
\catcode1=12
\catcode2=12
\mathcode1=\the\mathcode`\(
\delcode1=\the\delcode`\(
\mathcode2=\the\mathcode`\)
\delcode2=\the\delcode`\)
\makeparenactive
\everymath\expandafter{\the\everymath\let(^^A\let)^^B}
\everydisplay\expandafter{\the\everydisplay\let(^^A\let)^^B}
\def({\textup{\char`\(}}
\def){\textup{\char`\)}}
\makeparenother
\AtBeginDocument{% this is at begin document as it must be done
% after hyperref does its things
\makeparenactive
\let\zzzlabel\label
\let\zzzref\ref
\let\zzznewlabel\newlabel
\def\label{\makeparenletter\wwwlabel}
\def\ref{\makeparenletter\wwwref}
\def\newlabel{\makeparenletter\wwwnewlabel}
\def\wwwlabel#1{\makeparenactive\zzzlabel{#1}}
\def\wwwref#1{\makeparenactive\zzzref{#1}}
\def\wwwnewlabel#1{\makeparenactive\zzznewlabel{#1}}}
\begin{document}\thispagestyle{empty}
\begin{theorem}\label{(thm:1)}
I want parenthesis (around this text) to be upright. Of course, parentheses in
math mode are already upright: $\Bigg((E = mc^2)\Bigg)$, and we don't want to
fiddle with them.
\end{theorem}
\[ \Bigg((E = mc^2)\Bigg) \]
I want parenthesis (around this text) to be upright.
\emph{I want parenthesis (around this text) to be upright. \textbf{I want parenthesis (around this text) to be upright. }}
Theorem \ref{(thm:1)}
\end{document}
You can also use the pre_linebreak_filter
of LuaTeX. This also allows you to insert a little kerning around the parentheses. The solution below only works for \normalsize
but can easily be generalized, also to different fonts.
\documentclass{article}
\usepackage{hyperref}
\newtheorem{theorem}{Theorem}
\usepackage{luacode}
\begin{luacode*}
local open = 1
local close = 2
local match = {
[utf.byte("(")] = open,
[utf.byte(")")] = close,
[utf.byte("[")] = open,
[utf.byte("]")] = close,
}
fontmap = {}
local upright_parens = function(head)
for glyph in node.traverse_id(node.id("glyph"), head) do
if match[glyph.char] then
local replaced = false
if glyph.font == fontmap.it then
glyph.font = fontmap.tf
replaced = true
elseif glyph.font == fontmap.bfit then
glyph.font = fontmap.bf
replaced = true
end
if replaced and match[glyph.char] == open then
local kern = node.new("kern")
kern.kern = -.2 * glyph.width
node.insert_after(head, glyph, node.copy(kern))
end
if replaced and match[glyph.char] == close then
local kern = node.new("kern")
kern.kern = .2 * glyph.width
node.insert_before(head, glyph, node.copy(kern))
end
end
end
return head
end
luatexbase.add_to_callback("pre_linebreak_filter", upright_parens, "upright parens")
\end{luacode*}
\AtBeginDocument{
\textup{\directlua{fontmap.tf = font.current()}}
\textit{\directlua{fontmap.it = font.current()}}
\textbf{\directlua{fontmap.bf = font.current()}}
\textit{\textbf{\directlua{fontmap.bfit = font.current()}}}
}
\begin{document}\thispagestyle{empty}
\begin{theorem}\label{(thm:1)}
I want parenthesis (around this text) to be upright. Of course,
parentheses in math mode are already upright:
$\Bigg((E = mc^2)\Bigg)$, and we don't want to fiddle with them.
\end{theorem}
\[ \Bigg((E = mc^2)\Bigg) \] I want parenthesis (around this text) to
be upright. \emph{I want parenthesis (around this text) to be
upright. \textbf{I want parenthesis (around this text) to be
upright. }}
Theorem \ref{(thm:1)}
\end{document}