1

I'm trying to create a reusable style (let's call it titlerect) for a tikz node which is supposed to look something like the following figure I quickly whipped up in Inkscape:

enter image description here

I imagine it should be usable like

\node[titlerect, title={The title}] at (0,0) {The content};

Quite honestly, however, I don't even know where to start so there's currently no MWE I can provide. I would be grateful for any pointers on how I might go about creating such a style.

1
\documentclass[tikz,border=3.14mm]{standalone}
\begin{document}
\begin{tikzpicture}[titlerect/.style={draw,inner ysep=2ex,inner
xsep=1ex,minimum width={2*width("#1")},align=center,label={[anchor=center,fill=white,font=\Large\bfseries\sffamily]above:#1}}]
\node[titlerect={The title}] at (0,0) {The content\\ more content};
\end{tikzpicture}
\end{document}

enter image description here

You might be better off with tcolorbox, though.

  • 1
    Beautiful! Exactly what I was looking for. Maybe just as a remark: I'm using this style in a beamer presentation, so I had to replace each use of # with #### as explained here. – nkoep May 4 '19 at 6:09
  • @nkoep Thanks for letting me know! You could probably also do \tikzset{titlerect/.style={draw,inner ysep=2ex,inner xsep=1ex,minimum width={2*width("#1")},align=center,label={[anchor=center,fill=white,font=\Large\bfseries\sffamily]above:#1}}} in the preamble of the document, which would make the style global. – user121799 May 4 '19 at 6:16
  • Jepp, I did that, too. There's one issue that remains though. The title color is not affected by options passed to the node: \node[titlerect=title, green]. I also tried \node[titlerect=title, label={[text=green]}], which does not seem to work. The text inside the node is changed as desired, just not the label. – nkoep May 4 '19 at 10:40
  • I've solved it now by adding a second argument to the style via titlerect/.style 2 args={} and using it like \node[titlerect={title}{green}]. While this also requires specifying a color when a default one should be used, it's good enough for my purposes. – nkoep May 4 '19 at 11:00

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.