在适合的 TikZ 节点中对齐内容

在适合的 TikZ 节点中对齐内容

我有一个 TikZ 节点位于另外两个节点之间,使用fit

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

\pagestyle{empty}
\usepackage{parskip}

\newcommand{\mybox}[1]{
    \begin{tikzpicture}
        \node (t) [minimum width=.4\linewidth,anchor=north west,draw] at (0,5){box above};
        \node (b) [minimum width=.4\linewidth,anchor=south west,draw] at (0,0){box below};
        \node [fit=(t.south west) (b.north east), inner sep = 0, draw, align=flush left]{#1};
    \end{tikzpicture}
}

\begin{document}
\mybox{
    {\large Here we} have\newline some...

    ...somewhat {\bfseries longer arbitrary text} content.
}
\end{document}

截屏

现在我想将节点的内容垂直对齐到顶部,并在节点内容周围添加一些填充。我该怎么做?

答案1

我很乐意删除它,但我不明白你为什么需要fit在这里(也不明白为什么这是重复的)。

\documentclass{article}
\usepackage{tikz}

\pagestyle{empty}
\usepackage{parskip}

\newcommand{\mybox}[1]{\begin{tikzpicture}
        \node (t) [minimum width=.4\linewidth,anchor=north west,draw] at (0,5){box above};
        \node (b) [minimum width=.4\linewidth,anchor=south west,draw] at (0,0){box below};
        \node[text width=.4\linewidth-2ex,anchor=north west,inner
        xsep=1ex,inner ysep=1ex,align=flush left] at (t.south west){#1};
        \draw ([xshift=\pgflinewidth/2]t.south west) -- ([xshift=\pgflinewidth/2]b.north west) 
        ([xshift=-\pgflinewidth/2]t.south east) -- ([xshift=-\pgflinewidth/2]b.north east);
    \end{tikzpicture}}
\begin{document}
\mybox{
    {\large Here we} have\newline some...

    ...somewhat {\bfseries longer arbitrary text} content.
}
\end{document}

在此处输入图片描述

答案2

@marmot 答案的一个小变化(+1)。代码中的差异标记为% <---

\documentclass{article}
\usepackage{tikz}

\pagestyle{empty}
\usepackage{parskip}

\newcommand{\mybox}[1]{%
    \begin{tikzpicture}[
box/.style = {draw, text width=.4\linewidth, align=center, % <---
              inner sep=1ex, outer sep=0ex}                % <---
                        ]
\node (t) [box, below right] at (0,5){box above};
\node (b) [box, above right] at (0,0){box below};
\node[box, draw=none, align=flush left,                     % <---
      below] at (t.south) {#1};                             % <---
\draw (t.south west) rectangle (b.north east);              % <---
    \end{tikzpicture}}
\begin{document}
\mybox{
    {\large Here we} have\newline some...

    ...somewhat {\bfseries longer arbitrary text} content.
}
\end{document}

在此处输入图片描述

相关内容