forum icon indicating copy to clipboard operation
forum copied to clipboard

关于mathtools宏包dcases环境的行距问题

Open chenxiao445566 opened this issue 3 years ago • 1 comments

\documentclass{article}
\usepackage{mathtools}
\begin{document}
\[
\begin{dcases}
	x, & x>0,\\
	-x,& x<0.
\end{dcases}
 \quad 
\begin{dcases}
	x, & x>0,\\[5pt]
	-x,& x<0.
\end{dcases} 
 \quad 
 \begin{dcases}
  	x, & x>0,\\
  	0, & x=0,\\
  	-x,& x<0.
 \end{dcases}
\]
\end{document}

捕获 我用dcases环境排分段函数,如果是三行及以上,左括号包围的空白比较少;如果是两行,左括号包围的空白较多;当两行时,我手动加了\\[5pt],效果比较好。我的问题是,能否自动判断,假如只有两行,则行距自动加5pt?

chenxiao445566 avatar Jun 22 '22 04:06 chenxiao445566

利用 tabularray 宏包, 可以定义一个类似的 +dcases 环境,实现所需的结果:

\documentclass{article}
\usepackage{mathtools}
\usepackage{tabularray}
\UseTblrLibrary{amsmath}
\NewDocumentEnvironment{+dcases}{O{}+b}{
  \left\lbrace\begin{+array}{
    column{1} = {leftsep = 0pt}, column{Z} = {rightsep = 0pt},
    colspec = {Q[l,$$]Q[l,$$]}, stretch = 1, rowsep=1.5pt,
    hborder{2} = {
      abovespace = \ifnum\value{rowcount}>2 1.5pt\else 4pt\fi,
      belowspace = \ifnum\value{rowcount}>2 1.5pt\else 4pt\fi,
    },
    row{1} = {abovesep=0.5pt}, row{Z}={belowsep=0.5pt}, #1,
  }
    #2
  \end{+array}\right.
}{}
\begin{document}
\section{\texttt{dcases}}
\[
\begin{dcases}
	x, & x>0,\\[5pt]
	-x,& x<0.
\end{dcases}
 \quad
 \begin{dcases}
  	x, & x>0,\\
  	0, & x=0,\\
  	-x,& x<0.
 \end{dcases}
 \quad
 \begin{dcases}
  	\frac1x, & x>0,\\
  	0, & x=0,\\
  	x,& \frac1x<0.
 \end{dcases}
\]
\section{\texttt{+dcases}}
\[
\begin{+dcases}
	x, & x>0,\\
	-x,& x<0.
\end{+dcases}
 \quad
 \begin{+dcases}
  	x, & x>0,\\
  	0, & x=0,\\
  	-x,& x<0.
 \end{+dcases}
 \quad
 \begin{+dcases}
  	\frac1x, & x>0,\\
  	0, & x=0,\\
  	-x,& \frac1x<0.
 \end{+dcases}
\]
\end{document}

image

lvjr avatar Jun 23 '22 05:06 lvjr