2

I have a TikZ node positioned between two other nodes using 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}

screenshot

Now I would like to vertically align the contents of the node at the top and also add some padding around the node contents. How can I do this?

AndreKR
  • 1,025
  • Alternatively, you could just use a table. – schtandard Jun 07 '19 at 17:47
  • 1
    I reopened this because it is not a duplicate. (This is one of the few times I am using the "gold badge privilege", which I really do not like, but would encourage those who voted to close a post as a duplicate to carefully check it really is.) –  Jun 08 '19 at 00:56

2 Answers2

1

I will be happy to delete this but I do not see why you need fit here (nor why this is a duplicate).

\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}

enter image description here

1

A small variation of the @marmot answer (+1). Differences in code are marked by % <---:

\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}

enter image description here

Zarko
  • 296,517