2

I need some help to create boxes with two layers, color and text (as in the picture) that fit into textline. I thought about using tcolorbox but didn't work out for me yet. It is "needed" (it is not necessary but fancy ;) ) for working with the PCP-Algorithm (Theoretical computer science).

Hope someone has any good ideas.

boxes i would like to have.

  • Welcome to TeX.SX. Nice BVB colours :-P tcolorbox should do very well – user31729 Jun 23 '15 at 13:04
4

Obligatory solution with Tikz!

Output

figure 1

Code

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{shapes.multipart}

\tikzset{
    split/.style={rectangle split, rectangle split parts=2, draw}
}

\newcommand{\double}[2]{%
\tikz[baseline=(a.center)]{
    \tikz \node[split, anchor=west, yshift=-1mm, minimum width=1cm, inner sep=3pt, rectangle split part fill={black,yellow}] (a) {\textcolor{yellow}{#1}\nodepart{two}#2};
}}

\begin{document}\noindent
:\double{bbaa}{ba}, \double{ab}{abb} und \double{ab}{aabb}\\
:\double{0}{00}, \double{001}{11} und \double{1000}{0}
\end{document}
| improve this answer | |
  • @Dortmunder I see you're new, (welcome by the way), so remember to accept whichever answer solved your problem/you decided to adopt by clicking on the √ sign below the voting buttons. :) – Alenanno Jun 23 '15 at 14:15
4

The macro \bibox{top}{bottom} is what creates the box. By default (\biboxmode = 0), the box width is set to the content width. By resetting \biboxmode to something other than 0, it will make all boxes of fixed width, using the parameter \biboxwd.

\documentclass{article}
\usepackage{stackengine,xcolor}
\def\biboxmode{0}
\def\biboxwd{18pt}
\newcommand\bibox[3][\biboxmode]{%
  \setbox0=\hbox{\stackanchor{#2}{#3}}%
  \if0\biboxmode\else\wd0=\biboxwd\relax\fi%
  \setbox2=\hbox{\stackanchor[0pt]{\color{yellow}\colorbox{black}{\makebox[\wd0]{#2}}}%
  {\colorbox{yellow}{\makebox[\wd0]{#3}}}}%
  {\fboxsep=0pt\fbox{\box2}}%
}
\parskip 1ex
\begin{document}
: \bibox{bbaa}{ba}, \bibox{ab}{abb} und \bibox{ab}{aabb}\par
: \bibox{0}{00}, \bibox{001}{11} und \bibox{1000}{0}\par
Or else constant width:\def\biboxmode{1}\par
: \bibox{bbaa}{ba}, \bibox{ab}{abb} und \bibox{ab}{aabb}\par
: \bibox{0}{00}, \bibox{001}{11} und \bibox{1000}{0}
\end{document}

enter image description here

If one prefers the parameter \biboxwd to be not a "fixed", but rather a "minimum", box width (for \biboxmode other than 0), then this code accomplishes that:

\documentclass{article}
\usepackage{stackengine,xcolor}
\def\biboxmode{0}
\def\biboxwd{22pt}
\newcommand\bibox[3][\biboxmode]{%
  \setbox0=\hbox{\stackanchor{#2}{#3}}%
  \if0\biboxmode\else\ifdim\wd0<\biboxwd\relax\wd0=\biboxwd\relax\fi\fi%
  \setbox2=\hbox{\stackanchor[0pt]{\color{yellow}\colorbox{black}{\makebox[\wd0]{#2}}}%
  {\colorbox{yellow}{\makebox[\wd0]{#3}}}}%
  {\fboxsep=0pt\fbox{\box2}}%
}
\parskip 1ex
\begin{document}
: \bibox{bbaa}{ba}, \bibox{ab}{abb} und \bibox{ab}{aabb}\par
: \bibox{0}{00}, \bibox{001}{11} und \bibox{1000}{0}\par
Or else apply minimum width:\def\biboxmode{1}\par
: \bibox{bbaa}{ba}, \bibox{ab}{abb} und \bibox{ab}{aabbbbb}\par
: \bibox{0}{00}, \bibox{001}{11} und \bibox{1000}{0}
\end{document}

enter image description here

| improve this answer | |

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.