19

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).

  • Welcome to TeX.sx! Please add a minimal working example (MWE) that illustrates your problem. It will be much easier for us to reproduce your situation and find out what the issue is when we see compilable code, starting with \documentclass{...} and ending with \end{document}. – henrique Dec 8 '12 at 16:38
  • @henrique: I apologize. I thought the question was fairly obvious. I've added it now and I hope it's ok now. – Vedran Šego Dec 8 '12 at 16:48
  • 4
    I'm afraid that the only really robust solution is to have italic fonts with upright parentheses. I tend to avoid parentheses in italics contexts or use \textup(...\textup). – egreg Dec 8 '12 at 16:56
10

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

  • 4
    Try \def){\textup{\char`\)}} and you'll see an improvement. – egreg Dec 8 '12 at 17:31
  • @egreg thanks, modified the code. The spacing appears to be better, why is that? (I have to leave now) – user4686 Dec 8 '12 at 17:42
  • Thank you both, this seems to do a great job! For anyone else reading this, one of the things that breaks are labels and references with parentheses in them. These are, of course, easily edited if one knows what to looks for. – Vedran Šego Dec 8 '12 at 17:53
  • 6
    @jfbu \textup has heuristics for deciding whether to add an italic correction, which \upshape hasn't. – egreg Dec 8 '12 at 18:46
  • @egreg ok, didn't know that. – user4686 Dec 8 '12 at 23:20
2

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}

enter image description here

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.