5

Problem:

Trying to create a label for programming code by using text inside a color box inside another color box. However, I am not sure of the best practices regarding putting one box inside the other.

Minimal Working Example (MWE):

\documentclass{scrreprt}
\usepackage{xcolor}
\definecolor{editorLightGray}{cmyk}{0.05, 0.05, 0.05, 0.05}
\definecolor{editorPurple}{cmyk}{0.5, 1, 0, 0}
\usepackage[framemethod=tikz]{mdframed}

\begin{document}
    \begin{mdframed}[hidealllines=true,backgroundcolor=editorLightGray]
        PHP
    \end{mdframed}
\end{document}

This code produce the following outcome:

enter image description here

Desired outcome: enter image description here Update enter image description here

Any advice pointing in the right direction is appreciated.

1
  • Man that is beautifully formatted question.
    – henry
    Commented Jun 21, 2015 at 13:03

1 Answer 1

8

Here is a way using tcolorbox (which uses tikz in the backend)

\documentclass{scrreprt}
\usepackage{xcolor}
\definecolor{editorLightGray}{cmyk}{0.05, 0.05, 0.05, 0.05}
\definecolor{editorPurple}{cmyk}{0.5, 1, 0, 0}
\usepackage[most]{tcolorbox}
\usepackage{ragged2e}
\usepackage[framemethod=tikz]{mdframed}

\begin{document}
\raggedleft\begin{mdframed}[hidealllines=true,backgroundcolor=editorLightGray]
        PHP
    \end{mdframed}


\begin{tcolorbox}[boxrule=0pt,boxsep=0pt,right=5pt,colback=editorLightGray,arc=0pt,auto outer arc]
\RaggedLeft\begin{tcolorbox}[boxrule=0pt,coltext=white,colback=editorPurple,arc=0pt,auto outer arc,width=1.5cm,boxsep=0pt,left=5pt,right=5pt,halign=center]
\bfseries PHP
\end{tcolorbox}
\end{tcolorbox} % outer box

\begin{tcolorbox}[boxrule=0pt,boxsep=0pt,right=5pt,top=2pt,bottom=2pt,colback=editorLightGray,arc=0pt,auto outer arc]
\RaggedLeft\begin{tcolorbox}[boxrule=0pt,coltext=white,colback=magenta,arc=0pt,auto outer arc,width=1.5cm,boxsep=0pt,left=5pt,right=5pt,halign=center]
\bfseries PHP
\end{tcolorbox}
\end{tcolorbox} % outer box


\end{document}

enter image description here

Update Now as a command with a lot of control options etc.

\documentclass{scrreprt}
\usepackage{xcolor}
\definecolor{editorLightGray}{cmyk}{0.05, 0.05, 0.05, 0.05}
\definecolor{editorPurple}{cmyk}{0.5, 1, 0, 0}
\usepackage{calc}
\usepackage{xparse}
\usepackage{ragged2e}
\usepackage[most]{tcolorbox}
%\usepackage[framemethod=tikz]{mdframed}

\newlength{\titleboxwidth}

\newtcolorbox{codelanguagebox}[2][]{boxrule=0pt,coltext=white,colback=magenta,arc=0pt,auto outer arc,width={#2},boxsep=0pt,left=5pt,right=5pt,halign=center,#1}

\NewDocumentCommand{\codelabel}{O{}mO{}}{%
  \begin{tcolorbox}[boxrule=0pt,boxsep=0pt,right=5pt,top=2pt,bottom=2pt,colback=editorLightGray,arc=0pt,auto outer arc,#1]
    \setlength{\titleboxwidth}{\widthof{#2}}%
    \RaggedLeft\begin{codelanguagebox}[add to width=0.5cm,#3]{\the\titleboxwidth}
      \bfseries #2%
    \end{codelanguagebox}%
  \end{tcolorbox}   %
}


\begin{document}

\codelabel{PHP}

\codelabel{MySQL}

\codelabel[colback=blue]{MySQL}[colback=yellow,coltext=black]

\codelabel{C++}

\codelabel{\LaTeXe}


\end{document}

enter image description here

Last update

\documentclass{scrreprt}
\usepackage{xcolor}
\definecolor{editorLightGray}{cmyk}{0.05, 0.05, 0.05, 0.05}
\definecolor{editorPurple}{cmyk}{0.5, 1, 0, 0}
\usepackage{calc}
\usepackage{xparse}
\usepackage{ragged2e}
\usepackage[most]{tcolorbox}

\newlength{\titleboxwidth}

\newtcolorbox{codelanguagebox}[2][]{boxrule=0pt,coltext=white,colback=magenta,arc=0pt,auto outer arc,width={#2},boxsep=0pt,halign=center,valign=center,left=5pt,right=5pt,nobeforeafter,#1}

\NewDocumentCommand{\codelabel}{O{}mO{}}{%
  \begin{tcolorbox}[boxrule=0pt,boxsep=0pt,right=5pt,top=2pt,bottom=2pt,colback=editorLightGray,arc=0pt,auto outer arc,#1]
    \setlength{\titleboxwidth}{\widthof{#2}}%
    \RaggedLeft\begin{codelanguagebox}[add to width=0.5cm,#3]{\the\titleboxwidth}
      \bfseries #2%
    \end{codelanguagebox}%
  \end{tcolorbox}   %
}



\NewDocumentCommand{\twocodelabels}{O{}mO{}mO{}}{%
  \begin{tcolorbox}[boxrule=0pt,boxsep=0pt,right=5pt,top=2pt,bottom=2pt,colback=editorLightGray,arc=0pt,auto outer arc,#1]
    \RaggedLeft%
    \setlength{\topskip}{0pt}%
    \setlength{\titleboxwidth}{\widthof{#2}}%
      \begin{codelanguagebox}[add to width=0.5cm,#3]{\the\titleboxwidth}
      \bfseries #2%
    \end{codelanguagebox} 
    \begin{codelanguagebox}[#3,colback={gray}]{0.75cm}%
      \bfseries +
    \end{codelanguagebox} 
    \setlength{\titleboxwidth}{\widthof{#4}}%
    \begin{codelanguagebox}[#3,add to width=0.5cm,#5]{\the\titleboxwidth}%
      \bfseries #4%
    \end{codelanguagebox}%
\end{tcolorbox}%
}





\begin{document}
\codelabel{MySQL}
\codelabel{\scshape \huge PHP}
\codelabel{MySQL}[colback=editorPurple,coltext=black]
\twocodelabels{\ttfamily HTML}[height=0.6cm,colback=blue]{CSS}[colback=editorPurple]
\end{document}

enter image description here

20
  • I will update with a better version
    – user31729
    Commented Jun 21, 2015 at 11:48
  • Great solution, thanks. Is there a way to make the color box adjustable to the text? If I write for instance "MySQL" then the text shifts to the right. In addition, how do I control the height of the grey box?
    – kexxcream
    Commented Jun 21, 2015 at 11:53
  • 1
    @kexxcream: I am just about to do that ;-)
    – user31729
    Commented Jun 21, 2015 at 11:56
  • @kexxcream: See my update please
    – user31729
    Commented Jun 21, 2015 at 12:17
  • 1
    @kexxcream: Please note, that tcolorbox also has a nice interface for code listings, it uses listings in the backend, but can be configured to use minted as well
    – user31729
    Commented Jun 21, 2015 at 18:02

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .