例如,考虑以下代码:
\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{positioning,decorations.pathreplacing}
\begin{document}
\begin{tikzpicture}[estilo/.style={rectangle,draw,node distance=0mm,minimum width=0.5cm,minimum height=0.5cm}]
\tikzset
{
brace/.style= {decoration={brace, mirror, amplitude=8pt}, decorate}
}
\node[estilo] (n1) [minimum width=4cm]{};
\node[estilo] (n1-1)[ right=of n1.west, label=below:$i$ ]{};
\node[estilo] (n1-2)[ left=of n1.east, label=below:$j$ ]{};
\draw [brace] (n1-1.south west) -- (n1-2.south east)
node[midway, label=below:{$ n = j - i + 1 $}]{};
\end{tikzpicture}
\end{document}
输出:
我希望括号位于节点 n1-1 和 n1-2 的标签正下方,但采用相对形式。 任何想法?
答案1
如果您为j
标签命名(它只是一个节点),您可以使用它的南锚点来放置线。perpendicular cs:
此处有助于找到外角( 和 的i
宽度j
小于 0.5 厘米)。由于j
节点的深度较大,因此它也将用于支架的另一端。
代码
\documentclass[tikz]{standalone}
\usetikzlibrary{positioning,decorations.pathreplacing}
\begin{document}
\begin{tikzpicture}[
estilo/.style={
rectangle, draw, minimum size=+.5cm,
outer sep=+0pt, node distance=+0pt},% or: node distance=-.5\pgflinewidth
brace/.style= {decoration={brace, mirror, amplitude=+8pt}, decorate}
]
\node[estilo, minimum width=4cm] (n1){};
\node[estilo, right=of n1, label= below:$i$ ] (n1-1) {};
\node[estilo, left=of n1, label={[name=n1-2-j]below:$j$}] (n1-2) {};
\draw [brace] (n1-2.south west |- n1-2-j.south) -- (n1-1.south east |- n1-2-j.south)
node[midway, below=+\pgfdecorationsegmentamplitude]{$ n = j - i + 1 $};
\end{tikzpicture}
\end{document}