如何在整个文档中获得直立的括号?

如何在整个文档中获得直立的括号?

我的问题基本上和斜体文本中的直立括号:如何强制文档中的所有括号都直立,而无需“手动”用某些命令替换它们?

与链接的问题不同,我主要关心的是倾斜环境(即定理)。目前,我正在处理elsarticle,它使用了\itshape上述链接中的解决方案未涵盖的内容(我不知道如何修改它们以做到这一点),包中也没有embrac(第 9 节中明确说明)。

梅威瑟:

\documentclass{article}

\newtheorem{theorem}{Theorem}

\begin{document}

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

\end{document}

我当然可以写

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

或者为直立括号定义一些命令并使用它们。但是,更改 LaTeX 代码中的括号可能会导致错误(例如,遗漏一些括号并导致不一致),并且会让我更专注于细节,而不是我正在编写的文本,所以我更喜欢某种自动化方式(如\emph链接文档中显示的方式)。

答案1

正如 egreg 所警告的,下面的代码可能会破坏很多东西。但它在简单的情况下有效。[代码按照 egreg 的建议编辑]

[更新:在这个答案的底部,我编辑了答案以兼容\label\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}

文本模式下直立括号,数学模式下不变

根据 egreg 的建议,间距似乎更好:

在此处输入图片描述

代码(希望......)与\label和(在文档中使用\ref或未使用)兼容:hyperref

\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}

添加了标签和参考

答案2

您还可以使用pre_linebreak_filterLuaTeX 的。这也允许您在括号周围插入一些字距调整。下面的解决方案仅适用于\normalsize但可以轻松推广到不同的字体。

\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}

在此处输入图片描述

相关内容