我想创建“方括号”分隔符,看起来像这个问题。我想出了下面的代码,它产生了一些好的结果,但也有一些不便之处。例如,我们得到这些例子:
您可以看到分数线并非真正居中(因为指数“j”),并且求和符号下的格式太大。
我的问题是:
怎样才能得到好看的“方括号”呢?
梅威瑟:
\documentclass{article}
\usepackage{tikz, mathtools, titletoc}
\DeclareRobustCommand{\fp}[1]{%
\let\mybox\relax%
\newsavebox\mybox%
\sbox{\mybox}{$#1$}%
\def\WIDTH{\the\dimexpr\wd\mybox + 6pt \relax}%
\def\HEIGHT{\the\dimexpr\ht\mybox * 10 / 20 + \dp\mybox * 10/20 + 2pt\relax}%
\def\SHIFT{2pt}%
\begin{tikzpicture}[baseline = -0.57ex]
\draw [line width=0.6] (\SHIFT, \HEIGHT) -- (0, \HEIGHT) -- (0, 1.0pt) -- (-1.5pt, 0) -- (0, -1.0pt) -- (0, -\HEIGHT) -- (\SHIFT, -\HEIGHT);%
\node[anchor = west] at (-0.01, 0) {\copy\mybox};%
\draw [line width=0.6] (-\SHIFT + \WIDTH, \HEIGHT) -- (0 + \WIDTH, \HEIGHT) -- (0 + \WIDTH, 1.0pt) -- (1.5pt + \WIDTH, 0) -- (0 + \WIDTH, -1.0pt) -- (0 + \WIDTH, -\HEIGHT) -- (-\SHIFT + \WIDTH, -\HEIGHT);%
\end{tikzpicture}%
}
\begin{document}
$\fp{x} \qquad \fp{ \frac{x a}{d} } = \left\{ \frac{x a}{d} \right\} \leq \frac{1}{2}$
\[ \fp{ -x } = \fp{ \dfrac{a x}{d} } = \fp{ \frac{x}{2} } =
\fp{ \dfrac{- t \cdot a^j }{ R } } = \fp{ \dfrac{- t a_j }{ R } } \]
\[ \sum_{ \fp{r/m} \in S } r = \sum_{ \fp{ \frac{r}{m} } \in S } r. \]
\end{document}
答案1
这里有一个不同的方法。首先,我们定义一个tikz
样式sqbrace
,在点之间绘制新的括号。它有一个可选参数,用于将其垂直于其方向移动。例如,
\begin{tikzpicture}
\draw(0,0)--(2,1);
\draw[sqbrace=2](0,0)--(2,1);
\end{tikzpicture}
这2
支架移动 2 个单位,其中一个单位是支架的悬垂量(设置为1.5pt
。
然后我们定义一个宏\mybrace{<contents>}
,在包含的节点周围绘制括号<contents>
。该节点被vcenter
编辑并包含vphantom
内容,以使基线处于正确的位置。
\documentclass{article}
\usepackage{tikz, amsmath}
\usetikzlibrary{decorations.pathreplacing, calc}
\tikzset{
sqbrace/.style={decorate, decoration={show path construction,
lineto code={
\path (\tikzinputsegmentfirst); \pgfgetlastxy{\xstart}{\ystart}
\path (\tikzinputsegmentlast); \pgfgetlastxy{\xend}{\yend}
\path ($(0,0)!1.5pt!(\ystart-\yend,\xend-\xstart)$); \pgfgetlastxy{\xperp}{\yperp}
\path ($(0,0)!1.5pt!(\xend-\xstart, \yend-\ystart)$); \pgfgetlastxy{\xpar}{\ypar}
\draw[line width=.5pt, shift={(#1*\xperp,#1*\yperp)}] (\xstart-\xperp,\ystart-\yperp)--(\xstart,\ystart)--
([shift={(-.5*\xpar,-.5*\ypar)}]$.5*(\xstart,\ystart)+.5*(\xend,\yend)$)--
([shift={(.866*\xperp,.866*\yperp)}]$.5*(\xstart,\ystart)+.5*(\xend,\yend)$)--
([shift={(.5*\xpar,.5*\ypar)}]$.5*(\xstart,\ystart)+.5*(\xend,\yend)$)--
(\xend,\yend)--(\xend-\xperp,\yend-\yperp);
}
}},
sqbrace/.default={0}
}
\newcommand{\mybrace}[1]{{}\mathbin{\vcenter{\hbox{\tikz{
\node[inner ysep=.0pt, inner xsep=2pt](M){$#1\vphantom{\left(#1\right)}$};
\draw[sqbrace](M.north east)--(M.south east);
\draw[sqbrace](M.south west)--(M.north west);
}}}}{}}
\begin{document}
\[
\sum_{j=0}^N\mybrace{\dfrac{-t\cdot a^j}{R}}\qquad\sum_{\mybrace{\scriptstyle r/m}\in S}r\qquad\sum_{\mybrace{\frac{r}{m}}\in S}r
\]
\begin{tikzpicture}
\draw(0,0)--(2,1);
\draw[sqbrace=2](0,0)--(2,1);
\end{tikzpicture}
\end{document}
答案2
虽然不是那么优雅的解决方案,但它确实有效。最初,左括号和右括号与基线垂直对齐。之后,它们向上移动,.57ex
这是根据经验发现的。
\documentclass{article}
\usepackage{tikz, mathtools, titletoc}
\newsavebox\mybox
\DeclareRobustCommand{\fp}[1]{%
\text{\sbox{\mybox}{$#1$}%
\def\WIDTH{\the\dimexpr\wd\mybox + 6pt \relax}%
\def\HEIGHT{\the\dimexpr\ht\mybox * 10 / 20 + \dp\mybox * 10/20 + 2pt\relax}%
\def\SHIFT{2pt}%
\begin{tikzpicture}[baseline={([yshift=-.57ex]current bounding box.center)}]
\draw [line width=0.6] (\SHIFT, \HEIGHT) -- (0, \HEIGHT) -- (0, 1.0pt) -- (-1.5pt, 0) -- (0, -1.0pt) -- (0, -\HEIGHT) -- (\SHIFT, -\HEIGHT);%
\end{tikzpicture}%
\usebox{\mybox}%
\begin{tikzpicture}[baseline={([yshift=-.57ex]current bounding box.center)}]
\draw [line width=0.6] (-\SHIFT + \WIDTH, \HEIGHT) -- (0 + \WIDTH, \HEIGHT) -- (0 + \WIDTH, 1.0pt) -- (1.5pt + \WIDTH, 0) -- (0 + \WIDTH, -1.0pt) -- (0 + \WIDTH, -\HEIGHT) -- (-\SHIFT + \WIDTH, -\HEIGHT);%
\end{tikzpicture}}%
}
\begin{document}
$\fp{x} \qquad \fp{ \frac{x a}{d} } = \left\{ \frac{x a}{d} \right\} \leq \frac{1}{2}$
\[ \fp{ -x } = \fp{ \dfrac{a x}{d} } = \fp{ \frac{x}{2} } =
\fp{ \dfrac{- t \cdot a^j }{ R } } = \fp{ \dfrac{- t a_j }{ R } } \]
\[ \sum_{ \fp{r/m} \in S } r = \sum_{ \fp{ \frac{r}{m} } \in S } r. \]
\end{document}