1

Problem:

I am trying to create color containers that can hold text that vary in size. What I have not been able to figure is what method you should use in order to achieve this.

Example 1:

enter image description here

Example 2:

enter image description here

Desired outcome:

Be able to create a figure that allows text containers to dynamically expand based on text size. All examples follow the same setup: 3 boxes on top, 1 box in the middle, and one box at the bottom.

I would appreciate if someone could give pointers how/where to begin.

4

Using tikz:

The following simple macro typesets a colored box with some text:

\newcommand{\mycolorbox}[2][gray]{%
    \tikz[baseline=(n.base)] \node[rounded corners,%
                                   white,% Font color
                                   node font=\sffamily,% Sans serif font
                                   fill=#1,% Fill box with optional argument color
                                   inner xsep=5pt% Horizontal padding
                                  ](n){\mathstrut#2};%
}

Here is an example of use:

\documentclass[varwidth=5cm]{standalone}
\usepackage[T1]{fontenc}
\usepackage{tikz}
\newcommand{\mycolorbox}[2][gray]{\tikz[baseline=(n.base)] \node[rounded corners,white,node font=\sffamily,fill=#1,inner xsep=5pt](n){\mathstrut#2};}
\begin{document}
\mycolorbox[magenta]{while} \mycolorbox[cyan]{(condition)} \mycolorbox{\textbraceleft}\\
    \hspace*{15pt}\mycolorbox[orange]{Code}\\
\mycolorbox{\textbraceright}
\end{document}

rendering

  • Thank you kindly! But I have two questions 1) how do you increase the space above and under the orange box? 2) how do you increase the size of the entire colorbox? – kexxcream Aug 19 '15 at 15:17
  • 1
    You can increase the interline space, depending on how you use the boxes. To change the size of the boxes, you can set the inner xsep and inner ysep parameters in the \node call (they increase padding, if you are familiar with css). – TonioElGringo Aug 20 '15 at 7:28

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.