将文本与节点左上角对齐

将文本与节点左上角对齐

我有两个彼此下面的节点:

\documentclass[DIN, pagenumber=false, fontsize=11pt, parskip=half]{scrartcl}

\usepackage{tikz}
\usetikzlibrary{positioning}

\begin{document}

\begin{tikzpicture}[node distance=0pt, nodes={outer sep=0pt, minimum width=3cm, thick, minimum height=1cm}]

\node (n1) at (3,2) [draw, minimum height=2cm] {n1};
\node (n2)  [draw, below=of n1.south] {n2};
\node[left=of n1](t1) {this is a label};
\node[minimum width=0cm, left=of t1] {foo};

\end{tikzpicture}

\end{document}

我想在每个节点的左上角旁边添加两个相邻的标签。这些现在的行将标签放在节点旁边n1,但我无法将其向上推到角落。

\node[left=of n1](t1) {this is a label};
\node[minimum width=0cm, left=of t1] {foo};

上面的代码如下: 在此处输入图片描述

它应该是这样的: 在此处输入图片描述

答案1

您可以使用anchor=east, left=of n1.north west

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{positioning}

\begin{document}
\begin{tikzpicture}[node distance=0pt, nodes={outer sep=0pt, minimum width=3cm, thick, minimum height=1cm}]
\node (n1) at (3,2) [draw, minimum height=2cm] {n1};
\node (n2)  [draw, below=of n1.south] {n2};
\node[anchor=east, left=of n1.north west] (t1) {this is a label};
\node[minimum width=0cm, left=of t1] {foo};
\end{tikzpicture}
\end{document}

截屏

为了t1完全刷新节点n1,或调整它们之间的空间,请不要应用于,minimum width=3cm而是在其选项中t1使用类似的东西inner xsep=0, left=0cm of n1.north west。带有空分隔符(left=0cm of n1.north west)的示例:

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{positioning}

\begin{document}

\begin{tikzpicture}[node distance=0pt,
                    nodes={outer sep=0pt, thick, minimum height=1cm}]

\begin{scope}[minimum width=3cm]
  \node (n1) at (3,2) [draw, minimum height=2cm] {n1};
  \node (n2)  [draw, below=of n1.south] {n2};
\end{scope}

\node[inner xsep=0, anchor=east, left=0cm of n1.north west] (t1)
  {this is a label};
\node[minimum width=0cm, left=of t1] {foo};

\end{tikzpicture}

\end{document}

截屏

答案2

无需外部节点来放置这些标签。labels可用于此目的。 您只需选择正确的锚点和位置。 甚至可以声明labelslabels但在声明 的样式时要小心,nodes因为labels也是nodes

\documentclass[DIN, pagenumber=false, fontsize=11pt, parskip=half]{scrartcl}

\usepackage{tikz}
\usetikzlibrary{positioning}

\begin{document}

\begin{tikzpicture}[
    node distance=0pt, 
    box/.style={draw, outer sep=0pt, minimum width=3cm, thick, minimum height=1cm}]

\node[box, minimum height=2cm, 
    label={[name=t1, anchor=east, 
        label={[anchor=east]west:foo}]
            north west:this is a label}] 
    (n1) at (3,2) {n1};
\node[box, below=of n1.south] (n2) {n2};

\end{tikzpicture}

\end{document}

在此处输入图片描述

相关内容