4

i'm trying to get a gray background for an entire multicols environment and get errors with this "minimal" working example:

\documentclass[11pt,a4paper]{article}
\usepackage[utf8]{inputenc}
\usepackage{graphicx}
\usepackage[table,dvipsnames]{xcolor}
\usepackage{color}
\usepackage{listings}
\usepackage{multicol}
\usepackage[left=2.5cm,right=2.5cm]{geometry}

\begin{document}
  \setlength{\parindent}{0mm}
  \setlength{\parskip}{11pt}

  \begin{titlepage}
    \begin{center}
      \colorbox{gray}{
        \begin{multicols}{2}
          \begin{description}

            \item[xyz] \hfill \\
            description

            \item[xyz] \hfill \\
            description

            \columnbreak

            \item[xyz] \hfill \\
            description

            \item[xyz] \hfill \\
            description
          \end{description}
        \end{multicols}
      }
    \end{center}
  \end{titlepage}
\end{document}

This generates a lot of errors on the closing "}" from the colorbox that make no sense for me. If I comment the colorbox out it compiles without problems.

Something I guess is similar I hit while going through the suggestions: A version of colorbox that works inside math environments

Since one of the errormessages states that it's automatically creating a math environment/symbol i guess my problem is similar, though the answer to the question is just codesalad i don't understand and thus can't really use.

Any explained solution to either prevent LaTex to create a math-environment or getting a grey background under the multicol is highly appreciated.

Btw. i just started using LaTeX, so explanations are also highly appreciated.

2 Answers 2

3

\colorbox is an LR box like \mbox. You can not nest vertical material directly in such a box, use

  \noindent
  \colorbox{gray}{%
  \parbox{\dimexpr\textwidth-2\fboxsep\relax}{\begin{multicols}...
2
  • I just have one question left: which value does the \dimexpr... command return? And just to confirm that i understand it right: the \parbox creates a vertical box environment, which gives \multicols the environment it needs, right?
    – Chris
    Nov 22, 2012 at 10:10
  • it is an (etex but supported in all current distributuons) syntax which expands to the value given using infix syntax (an alternative to using the calc package) you want a full width box but the \parbox can not be \textwidth wide as \colorbox like \fbox adds padding all round so there is an additional \fboxsep space added to each side that you need to subtract to get the size needed for the inner box. Nov 22, 2012 at 10:12
2

Here are some methods you can use:

  • the tcolorbox package;
  • the mdframed package;
  • a tikzmark-based solution (requires the library tikzmark - download the .dtx and compile to get the needed tikzlibrarytikzmark.code.tex).

The code:

\documentclass[11pt,a4paper]{article}
\usepackage[utf8]{inputenc}
\usepackage{multicol}
\usepackage[left=2.5cm,right=2.5cm]{geometry}
\usepackage{tcolorbox}
\usepackage{mdframed}
\usepackage{xparse}
\usepackage{tikz}
\usetikzlibrary{tikzmark}

\NewDocumentCommand{\tikzbox}{m D(){0.1,-0.18} D(){-0.1,0.35}}{%
      \tikz[remember picture,overlay]
      \draw[line width=1pt,rectangle,rounded corners,fill=lightgray!50,draw=white]
      (pic cs:#1) ++(#2) rectangle (#3)
      ;}

\begin{document}
  \setlength{\parindent}{0mm}
  \setlength{\parskip}{11pt}

% With tcolorbox

  \begin{titlepage}
    \begin{tcolorbox}[colframe=white,colback=lightgray!50] 
        \begin{multicols}{2}
          \begin{description}

            \item[xyz] \hfill \\
            description

            \item[xyz] \hfill \\
            description

            \columnbreak

            \item[xyz] \hfill \\
            description

            \item[xyz] \hfill \\
            description
          \end{description}
        \end{multicols}
      \end{tcolorbox}
  \end{titlepage}

% Using mdframed

   \begin{titlepage}
    \begin{mdframed}[backgroundcolor=lightgray!50,linecolor=white]
        \begin{multicols}{2}       
          \begin{description}
            \item[xyz] \hfill \\
            description

            \item[xyz] \hfill \\
            description

            \columnbreak

            \item[xyz] \hfill \\
            description

            \item[xyz] \hfill \\
            description
          \end{description}
        \end{multicols}
      \end{mdframed}  
  \end{titlepage}

     \begin{titlepage}
        \begin{multicols}{2}

          \begin{description}
            \item[\tikzbox{a}xyz]   \hfill \\
            description

            \item[xyz] \hfill \\
            description

            \columnbreak

            \item[xyz] \hfill \\
            description

            \item[xyz] \hfill \\
            description    \tikzmark{a}        
          \end{description}          
        \end{multicols}
  \end{titlepage}

\end{document}

The three results:

  • with tcolorbox:

enter image description here

  • with mdframed:

enter image description here

  • with tikzmark:

enter image description here

Notice that the tikzmark solution provides a strict box around the text: if you want to extend, it is possible, you can refer to Issues and potentiality of the tikzmark macro: dynamic box adaptation because the \tikzbox command has the so called extensible markers.

You must log in to answer this question.

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