花括号:在每个括号周围添加半空格

花括号:在每个括号周围添加半空格

当我使用命令\{和时\},呈现的括号“太紧”或太靠近它们之间的文本和数学。我想重新定义\{\{\,\}\,\}但我不确定如何使用分隔符来做到这一点。

答案1

一个低技术的解决方案是定义一个新命令,例如:

\newcommand{\Set}[1]{\{\,#1\,\}}

以下是带图片的 MWE:

\documentclass{article}
   \newcommand{\Set}[1]{\{\,#1\,\}}
\begin{document}
   \{a, b\} vs \Set{a, b} 

   $\{a, b\}$ vs $\Set{a, b}$
\end{document}

在此处输入图片描述

答案2

\,可以通过重新定义\{和来添加额外的空格(例如) \}。但它不适用于\},它会破坏\right,因为空格会插入到\right和 分隔符之间。因此,该示例重新定义\right\}在原始 之前的正确位置插入空格\right。该示例还假设仅在数学模式下才需要空格,花括号的文本模式版本不会更改。

\documentclass{article}

\DeclareRobustCommand{\{}{\ifmmode\lbrace\,\else\textbraceleft\fi}
\DeclareRobustCommand{\}}{\ifmmode\rbracespace\else\textbraceright\fi}
\newcommand*{\rbracespace}{\,\rbrace}

\makeatletter
\let\saved@right\right
\renewcommand*{\right}{%
  \@ifnextchar\}{\,\saved@right\rbrace\@gobble}{\saved@right}%
}
\makeatletter

\begin{document}
  \[ \{ a, b \} = \left\{ \frac{c}{d}, \frac{e}{f} \right\} \]
\end{document}

结果

\big和朋友的进一步支持

\documentclass{article}

\DeclareRobustCommand{\{}{\ifmmode\lbrace\,\else\textbraceleft\fi}
\DeclareRobustCommand{\}}{\ifmmode\rbracespace\else\textbraceright\fi}
\newcommand*{\rbracespace}{\,\rbrace}

\makeatletter
\newcommand*{\patch@for@rbrace}[1]{%
  % #1: command to be patched without backslash
  \expandafter\@patch@for@rbrace
  \csname saved@#1\expandafter\endcsname
  \csname #1\endcsname
}
\newcommand*{\@patch@for@rbrace}[2]{%
  % #1: macro for original meaning
  % #2: command to be patched
  \@ifdefinable{#1}{%
    \let#1=#2%
    \renewcommand*{#2}{%
      \@ifnextchar\}{\,#1\rbrace\@gobble}{#1}%
    }%
  }%
}
\@for\x:=right,bigr,Bigr,biggr,Biggr,big,Big,bigg,Bigg\do{%
  \patch@for@rbrace\x
}
\renewcommand*{\bigl}{\mathopen\saved@big}
\renewcommand*{\Bigl}{\mathopen\saved@Big}
\renewcommand*{\biggl}{\mathopen\saved@bigg}
\renewcommand*{\Biggl}{\mathopen\saved@Bigg}
\renewcommand*{\saved@bigr}{\mathclose\saved@big}
\renewcommand*{\saved@Bigr}{\mathclose\saved@Big}
\renewcommand*{\saved@biggr}{\mathclose\saved@bigg}
\renewcommand*{\saved@Biggr}{\mathclose\saved@Bigg}
\makeatletter

\begin{document}
  \[ \{ a, b \} = \left\{ \frac{c}{d}, \frac{e}{f} \right\} \]
  \[ \bigl\{ \mbox{big l/r} \bigr\} \]
  \[ \Bigl\{ \mbox{Big l/r} \Bigr\} \]
  \[ \biggl\{ \mbox{bigg l/r} \biggr\} \]
  \[ \Biggl\{ \mbox{Bigg l/r} \Biggr\} \]
  \[ \big\{ \mbox{big} \big\} \]
  \[ \Big\{ \mbox{Big} \Big\} \]
  \[ \bigg\{ \mbox{bigg} \bigg\} \]
  \[ \Bigg\{ \mbox{Bigg} \Bigg\} \]
\end{document}

结果

相关内容