编辑:我想将yshift
节点的设置为节点内容行数的某个因子。在尝试确定行数时,我最初写了下面的问题。
编辑下面是基于薛定谔猫答案的部分解决方案,包括图片和代码。它不是完全自动化的,指定框和在每个节点之前使用\pgftruncatemacro
是不可取的。
\documentclass[margin=6]{standalone}
\usepackage{tikz}
\usetikzlibrary{positioning,fit,calc}
\begin{document}
\newbox\mybox\setbox\mybox=\vbox{\hbox{foo}\hbox{bar}\hbox{bazzy}}
\newbox\myboxb\setbox\myboxb=\vbox{\hbox{\ttfamily abc\#xyz}}
\begin{tikzpicture}[
card/.style={
draw,
dashed,
anchor=base west,
inner sep=0,
append after command={[/utils/exec=\let\mytikzlastnode\tikzlastnode]
node [
below=5mm of \tikzlastnode.base east,
anchor=base east,
font=\scriptsize,
inner sep=0
] (mynode) {#1}
node [draw,fit={(\mytikzlastnode) (mynode.base east)}] {}
},
}
]
\draw (-1,0) -- (5,0);
\pgfmathtruncatemacro{\myn}{(\dp\mybox+\ht\mybox+2pt)/11.5pt}\myn
\node [card=num1,yshift=-(\myn-1)*\baselineskip] (A) {\box\mybox};
\pgfmathtruncatemacro{\myn}{(\dp\myboxb+\ht\myboxb+2pt)/11.5pt}\myn
\node [card=c2,right=1cm of A,yshift=-(\myn-1)*\baselineskip] (B) {\box\myboxb};
\end{tikzpicture}
\end{document}
原始问题:我想将节点内容中的行数附加为“辅助”节点,如下所示的 MWE。
我有一个候选解决方案来计算某些行数这个答案,但我无法很好地解析代码,无法从中解开该解决方案\title
。我找不到从中访问节点内容的方法style
。
\documentclass[margin=6]{standalone}
\usepackage{tikz}
\usetikzlibrary{positioning,fit,calc}
\begin{document}
\begin{tikzpicture}[
card/.style={
draw,
dashed,
inner sep=0,
append after command={
\pgfextra{
\node [
below=5mm of \tikzlastnode.base east,
anchor=base east,
font=\scriptsize,
inner sep=0
] (mynode) {????}; % get node contents and count lines
\node [draw,fit={(\tikzlastnode) (mynode.base east)}] {};
}
}
}
]
\node [card] (C) {foobar\\baz};
\end{tikzpicture}
\end{document}
答案1
我不知道用 Ti 测量线数的 100% 万无一失的方法钾Z. 如果你有一个很大的积分符号怎么办?但是,对于正常线,你可以测量高度并从这个高度推断出数字。顺便说一句,不是\pgfextra
在路径中使用。
\documentclass[margin=6]{standalone}
\usepackage{tikz}
\usetikzlibrary{positioning,fit,calc}
\begin{document}
\begin{tikzpicture}[
card/.style={align=center,
draw,
dashed,
inner sep=0,
append after command={[/utils/exec=\let\mytikzlastnode\tikzlastnode]
let \p1=($(\mytikzlastnode.north)-(\mytikzlastnode.south)$) in
node [
below=5mm of \tikzlastnode.base east,
anchor=base east,
font=\scriptsize,
inner sep=0
] (mynode) {\pgfmathtruncatemacro{\myn}{(\y1+5pt)/11.5pt}\myn} % get node contents and count lines
node [draw,fit={(\mytikzlastnode) (mynode.base east)}] {}
}
}
]
\draw node [card] (A) {foobar};
\draw (3,0) node [card] (B) {foobar\\baz};
\draw (6,0) node [card] (C) {foobar\\baz\\blub};
\draw (9,0) node [card] (D) {foobar\\baz\\blub\\pft};
\end{tikzpicture}
\end{document}