TikZ,矩形中的基线并仅绘制轮廓的三边

TikZ,矩形中的基线并仅绘制轮廓的三边

使用 Tikz,将三个带有虚线轮廓的矩形放置在彼此正上方,如何让 tikz 不多次绘制它们之间的线条?

此外,这些矩形内的文本基线在框内的高度并不总是相同的 - 这取决于文本是否使用大写线(如“b”)、下降线(如“q”)或非下降线(如“a”)。

梅威瑟:

\documentclass{standalone}
\usepackage{tikz}
\begin{document}
\begin{tikzpicture}

  \node (A) [draw,rectangle,minimum width=37mm, minimum height=9mm, anchor=north, dashed] {a};
  \node (B) at (A.south) [draw,rectangle,minimum width=37mm, minimum height=9mm, anchor=north, yshift=+0.4pt, dashed] {b};
  \node (Q) at (B.south) [draw,rectangle,minimum width=37mm, minimum height=9mm, anchor=north, yshift=+0.4pt, dashed] {q};

\end{tikzpicture}
\end{document}

答案1

您不必绘制每个节点,而是可以在放置边框和分隔线后绘制它们:

\documentclass{standalone}
\usepackage{tikz}
\usetikzlibrary{positioning, fit}
\begin{document}
\begin{tikzpicture}[box/.style={minimum width=37mm, minimum height=9mm},
    line/.style={dashed}]

  \node (A) [box] {a};
  \node (B) [box, below=0pt of A] {b};
  \node (Q) [box, below=0pt of B] {q};
    \node[fit=(A) (Q), draw, line, inner sep=0pt] (ABQ) {};
    \draw[line] (A.south-|Q.west)--(A.south-|Q.east) (B.south-|Q.west)--(B.south-|Q.east);
\end{tikzpicture}
\end{document}

在此处输入图片描述

fit您还可以使用 以下方法来代替节点:\draw[line] (A.north west) rectangle (Q.south east);

相关内容