缩短括号(或填充 tikz 括号装饰)

缩短括号(或填充 tikz 括号装饰)

这样做感觉有点奇怪,但我发现这个尚未解答的问题:PGF 和 TikZ(邮件列表)- 缩短括号,由于我对答案很感兴趣,因此我将重新发布:

很多时候我需要对连续的(矩形)形状组进行一些描述,即如下所示:[下面]
我喜欢在连续的括号之间留出较小的空间而不改变括号坐标。所以我尝试使用缩短,但结果只是部分令人满意......(参见括号 C 和 D)。好吧,如果我将缩短设置为 0.5 点,括号看起来很完美,但它们之间的距离太小了。

还有人有更好的解决方案吗?

以下是示例的输出:

测试41

我添加了红色括号作为首选解决方案的示例(对我来说),其中它有 1 pt 的“填充”;shortened 括号位于其下方,看起来不太好。红色括号看起来不错,但它是通过calc手动偏移端点来实现的,对我来说,输入起来有点乏味。

那么,有没有更好的方法来实现“缩短”或“加垫”的支架呢?

以下是 MWE(带有红色括号修改):

\documentclass[tikz]{standalone}
\usetikzlibrary{decorations.pathreplacing}
\usetikzlibrary{calc}
\begin{document}
\begin{tikzpicture}[
box/.style = {draw, minimum size=9pt,inner sep=1pt, outer sep=0pt, right},
brc/.style = {decorate, decoration={brace,raise=1mm}},
shorten <>/.style = {shorten >=#1pt, shorten <=#1pt}
                     ]
\coordinate (a0) at (0,0);
\foreach \x [count=\xi from 0] in {1,...,4}
   \node[box] (a\x) at (a\xi.east) {\x};
\draw[brc] (a1.north west) to node[above=2mm] {A} (a2.north east);
\draw[brc] (a3.north west) to node[above=2mm] {B} (a4.north east);
\def\brcpad{1pt}
\draw[brc, draw=red, decoration={raise=0.5mm}] ($(a2.south east)+(-\brcpad,0pt)$) -- ($(a1.south west)+(\brcpad,0pt)$);
\draw[brc,shorten <>=1] (a2.south east) to node[below=2mm] {C} (a1.south
west);
\draw[brc,shorten <>=1] (a4.south east) to node[below=2mm] {D} (a3.south
west);
\end{tikzpicture}
\end{document}

答案1

明白了 - 但有点棘手,因为遇到了一个 bug;tikz:如何镜像路径装饰“沿路径的文本”?

b):一旦我包含prepre length,其他选项(例如raisemirror)似乎不再受到尊重。

这实际上是PGF 和 TikZ - TeX 的图形系统 / Bug / #116 预/后使用时装饰无法重新定位在网上搜索到raise代码后 tikzlibrarydecorations.code.tex,我设法找到了错误 #116 的修复差异:

否则,答案是 - 可以使用pre/ post(及其length)来“缩短”括号装饰;因此在下面的 MWE 中,有一种样式,它通过和brcb指定 1 pt 的“填充” ;并输出以下图像:pre lengthpost length

测试42.png

(该brcb样式应用于“D”形支架;其凸起 0.5 毫米,以便与红色支架进行比较)

以下是代码——包括粘贴的错误修复:

\documentclass[tikz]{standalone}
\usetikzlibrary{decorations.pathreplacing}
\usetikzlibrary{calc}
% copied fix for [http://sourceforge.net/p/pgf/bugs/116/ PGF and TikZ -- Graphic systems for TeX / Bugs / #116 Decorations can't be repositioned when pre/post used]
% http://pgf.cvs.sourceforge.net/viewvc/pgf/pgf/generic/pgf/frontendlayer/tikz/libraries/tikzlibrarydecorations.code.tex?r1=1.14&r2=1.15
\makeatletter
\pgfdeclaremetadecoration{tikz@internal}{pre}{
  \state{pre}[width=\pgfkeysvalueof{/pgf/decoration/pre length}, next state=main]
  {
    \tikz@dec@trans
    \decoration{\pgfkeysvalueof{/pgf/decoration/pre}}
  }
  \state{main}[width=\pgfmetadecoratedremainingdistance-\pgfkeysvalueof{/pgf/decoration/post length}, next state=final]
  {
    \tikz@dec@trans
    \decoration{\tikz@decoration@name}
  }
  \state{final}
  {
    \tikz@dec@trans
    \decoration{\pgfkeysvalueof{/pgf/decoration/post}}
  }
}
\makeatother
\begin{document}
\begin{tikzpicture}[
box/.style = {draw, minimum size=9pt,inner sep=1pt, outer sep=0pt, right},
brc/.style = {decorate, decoration={brace,raise=1mm}},
brcb/.style = {decoration={brace,pre=moveto,pre length=1pt,post=moveto,post length=1pt,raise=0.5mm}, decorate},
shorten <>/.style = {shorten >=#1pt, shorten <=#1pt}
]
\coordinate (a0) at (0,0);
\foreach \x [count=\xi from 0] in {1,...,4}
   \node[box] (a\x) at (a\xi.east) {\x};
\draw[brc] (a1.north west) to node[above=2mm] {A} (a2.north east);
\draw[brc] (a3.north west) to node[above=2mm] {B} (a4.north east);
\def\brcpad{1pt}
\draw[brc, draw=red, decoration={raise=0.5mm}] ($(a2.south east)+(-\brcpad,0pt)$) -- ($(a1.south west)+(\brcpad,0pt)$);
\draw[brc,shorten <>=1] (a2.south east) to node[below=2mm] {C} (a1.south
west);
\draw[brcb] (a4.south east) to node[below=2mm] {D} (a3.south west);
\end{tikzpicture}
\end{document}

答案2

好吧,我不是一个tikz人,所以如果我的语法有缺陷,我很抱歉,但在这里我提供了\downbrace[max.-height]{width}\upbrace[max.-height]{width}。语法相对简单易写,但必须明确指定尺寸。虽然有些事情可能很难,但在这里您将框指定为边缘 9pt,因此括号跨度将是 2x9pt - 2pt 缓冲区 = 16pt 长。对于这个 MWE,我用蓝色显示它们。我还使用了两个不同的最大高度,再次说明。

\documentclass[tikz]{standalone}
\usetikzlibrary{decorations.pathreplacing}
\usetikzlibrary{calc}
\usepackage{scalerel}
\newcommand\downbrace[2][1ex]{\textcolor{blue}{\rotatebox{90}{\scaleto[#1]{\}}{#2}}}}
\newcommand\upbrace[2][1ex]{\textcolor{blue}{\rotatebox{90}{\scaleto[#1]{\{}{#2}}}}
\begin{document}
\begin{tikzpicture}[
box/.style = {draw, minimum size=9pt,inner sep=1pt, outer sep=0pt, right},
brc/.style = {decorate, decoration={brace,raise=1mm}},
shorten <>/.style = {shorten >=#1pt, shorten <=#1pt}
                     ]
\coordinate (a0) at (0,0);
\foreach \x [count=\xi from 0] in {1,...,4}
   \node[box] (a\x) at (a\xi.east) {\x};
\draw[brc] (a1.north west) to node[above=2mm] {A} (a2.north east);
\draw[brc] (a3.north west) to node[above=2mm] {B} (a4.north east);
\def\brcpad{1pt}
\draw[brc, draw=red, decoration={raise=0.5mm}] ($(a2.south east)+(-\brcpad,0pt)$) -- ($(a1.south west)+(\brcpad,0pt)$);
\draw (a1.north west) to node[above=-1.1mm] {\downbrace{16pt}} (a2.north east);
\draw (a3.south west) to node[below=-1mm] {\upbrace[.9ex]{16pt}} (a4.south east);
\draw[brc,shorten <>=1] (a2.south east) to node[below=2mm] {C} (a1.south
west);
\draw[brc,shorten <>=1] (a4.south east) to node[below=2mm] {D} (a3.south
west);
\end{tikzpicture}
\end{document}

在此处输入图片描述

相关内容