[Tex/LaTex] How to get upright parentheses in the whole document

fontsitalicpunctuationtypography

My question is, basically, the same as the one in Upright parentheses in italic text: how do I force all parentheses in my document to be upright, without replacing them with some commands "by hand"?

Unlike the linked question, my main concern are slanted environments (i.e., theorems). At the moment, I'm dealing with elsarticle, which uses \itshape which is not covered by the solutions on above link (and I don't see how I could modify them to do so), nor by the embrac package (explicitly stated in Section 9).

MWE:

\documentclass{article}

\newtheorem{theorem}{Theorem}

\begin{document}

\begin{theorem}
I want parenthesis (around this text) to be upright.
\end{theorem}

\end{document}

I can, of course, write

I want parenthesis {\rm (}around this text{\rm )} to be upright.

or define some commands for the upright parentheses and use them. However, changing the parentheses in the LaTeX code can lead to errors (for example, missing some of them and introducing inconsistency) and puts a burden of focusing on that detail, instead of the text I'm writing, so I'd prefer some automated way (like the one shown for \emph in the linked document).

Best Answer

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}

upright parenthesis in text mode, math mode unchanged

with egreg's suggestion the spacing appears to be better:

enter image description here

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}

with label and ref added