Tikz – Box/frame arround Text with interruption

Something like this? The vertical distances are controlled by inner ysep, and the horizontal ones by inner xsep.

\documentclass[tikz,border=3mm]{standalone}
\begin{document}
\begin{tikzpicture}[font=\sffamily\bfseries]
 \node[scale=4,red!60,inner xsep=1.5ex,inner ysep=0.5ex] (2019){2019};
 \node[above,scale=2,gray!50] at (2019.north) (A) {Name xy};
 \node[below,scale=2,gray!50] at (2019.south) (B) {Name};
 \draw[thick,gray!50] (A.east) -| (2019.east|-B) -- (B.east)
 (A.west) -| (2019.west|-B) -- (B.west);
\end{tikzpicture}
\end{document}

enter image description here


Just for fun a TikZ-less version, that only uses xcolor and grabbox (grabbox wouldn't really be necessary, but this way you can use \verb and stuff in the braced arguments).

Argument structure of \boxtext is:

  • optional argument specifying fixed width of top-text
  • mandatory argument for top-text
  • optional argument specifying fixed width of main-text
  • mandatory argument for main-text
  • optional argument specifying fixed width of bottom-text
  • mandatory argument for bottom-text

If you don't specify a fixed width the natural width is used, if a fixed width is specified the contents will be centred using \centering.

\documentclass[]{article}

\usepackage{xcolor}
\usepackage{grabbox}

\makeatletter
\colorlet{boxtextborder}{gray}
\newlength\toptextpad
\newlength\maintextpad
\newlength\bottomtextpad
\newlength\boxtextborder
\setlength\toptextpad{2pt}
\setlength\maintextpad{5pt}
\setlength\bottomtextpad{2pt}
\setlength\boxtextborder{.4pt}
\newlength\boxtext@maxwd
\newsavebox\boxtext@toptext
\newsavebox\boxtext@maintext
\newsavebox\boxtext@bottomtext
\def\myfi@Tb\fi#1#2#3{\fi#2}
\newcommand\boxtext@ifrelax[1]
  {%
    \ifx\relax#1%
      \myfi@Tb
    \fi
    \@secondoftwo
  }
\newcommand\boxtext[1][\relax]
  {%
    \boxtext@ifrelax{#1}
      {\@grabbox{}\boxtext@toptext{\color{boxtextborder}}\hbox}
      {\@grabbox{\hsize#1\centering}\boxtext@toptext{\color{boxtextborder}}\vbox}%
    {}\boxtext@a
  }
\newcommand\boxtext@a[1][\relax]
  {%
    \boxtext@ifrelax{#1}
      {\@grabbox{}\boxtext@maintext{}\hbox}
      {\@grabbox{\hsize#1\centering}\boxtext@maintext{}\vbox}%
    {}\boxtext@b
  }
\newcommand\boxtext@b[1][\relax]
  {%
    \boxtext@ifrelax{#1}
      {\@grabbox{}\boxtext@bottomtext{\color{boxtextborder}}\hbox}
      {\@grabbox{\hsize#1\centering}\boxtext@bottomtext{\color{boxtextborder}}\vbox}%
    {}\boxtext@out
  }
\newcommand*\boxtext@siderule
  {%
    \vrule
      height
        \dimexpr
          \ht\boxtext@maintext+.5\ht\boxtext@toptext+.5\dp\boxtext@toptext
          +\toptextpad+\maintextpad+.5\boxtextborder
        \relax
      depth
        \dimexpr
          \dp\boxtext@maintext+.5\ht\boxtext@bottomtext+.5\dp\boxtext@bottomtext
          +\maintextpad+\bottomtextpad+.5\boxtextborder
        \relax
      width \boxtextborder
    \relax
  }
\newcommand*\boxtext@toprule
  {%
    \vrule
      width
        \dimexpr
          (\boxtext@maxwd-\wd\boxtext@toptext)/2-\toptextpad
        \relax
      height
        \dimexpr
          \ht\boxtext@maintext+.5\ht\boxtext@toptext+.5\dp\boxtext@toptext
          +\toptextpad+\maintextpad+.5\boxtextborder
        \relax
      depth
        -\dimexpr
          \ht\boxtext@maintext+.5\ht\boxtext@toptext+.5\dp\boxtext@toptext
          +\toptextpad+\maintextpad-.5\boxtextborder
        \relax
    \relax
  }
\newcommand*\boxtext@bottomrule
  {%
    \vrule
      width
        \dimexpr
          (\boxtext@maxwd-\wd\boxtext@bottomtext)/2-\bottomtextpad
        \relax
      height
        -\dimexpr
          \dp\boxtext@maintext+.5\ht\boxtext@bottomtext+.5\dp\boxtext@bottomtext
          +\maintextpad+\bottomtextpad-.5\boxtextborder
        \relax
      depth
        \dimexpr
          \dp\boxtext@maintext+.5\ht\boxtext@bottomtext+.5\dp\boxtext@bottomtext
          +\maintextpad+\bottomtextpad+.5\boxtextborder
        \relax
    \relax
  }
\newcommand*\boxtext@out
  {%
    \mbox
      {%
        \begingroup
        \ifdim\wd\boxtext@maintext>\wd\boxtext@toptext
          \ifdim\wd\boxtext@maintext>\wd\boxtext@bottomtext
            \boxtext@maxwd\wd\boxtext@maintext
          \else
            \boxtext@maxwd\wd\boxtext@bottomtext
          \fi
        \else
          \ifdim\wd\boxtext@toptext>\wd\boxtext@bottomtext
            \boxtext@maxwd\wd\boxtext@toptext
          \else
            \boxtext@maxwd\wd\boxtext@bottomtext
          \fi
        \fi
        \advance\boxtext@maxwd2\maintextpad
        \color{boxtextborder}%
        \boxtext@siderule
        \rlap{\makebox[\boxtext@maxwd][c]{\usebox\boxtext@maintext}}%
        \rlap
          {%
            \makebox[\boxtext@maxwd][c]
              {%
                \raisebox
                  {%
                    \dimexpr
                      \ht\boxtext@maintext+\dp\boxtext@toptext
                      +\maintextpad+\toptextpad
                  }
                  {\usebox\boxtext@toptext}%
              }%
          }%
        \rlap
          {%
            \makebox[\boxtext@maxwd][c]
              {%
                \raisebox
                  {%
                    -\dimexpr
                      \dp\boxtext@maintext+\ht\boxtext@bottomtext
                      +\maintextpad+\bottomtextpad
                  }
                  {\usebox\boxtext@bottomtext}%
              }%
          }%
        \rlap{\boxtext@toprule}%
        \rlap{\boxtext@bottomrule}%
        \hskip\boxtext@maxwd
        \llap{\boxtext@toprule}%
        \llap{\boxtext@bottomrule}%
        \boxtext@siderule
        \endgroup
      }%
  }
\makeatother

\begin{document}
\boxtext
  {Name xy}
  {\textcolor{red}{2019}}
  {Name}%

\boxtext
  {\textsf{Name xy}}
  {{\sffamily\bfseries\huge\textcolor{red}{2019}}}
  {\textsf{Name}}

\boxtext{Name xy}[5cm]{This is some text that is 5cm wide}{Name}
\end{document}

enter image description here

Tags:

Box

Tikz Pgf