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?

  • Alternatively, you could just use a table. – schtandard Jun 7 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.) – user121799 Jun 8 at 0:56
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

  • That will indeed work. – AndreKR Jun 7 at 18:23
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

  • +1 for being shorter. – AndreKR Jun 8 at 7:06

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.