3

I am trying to create an enviroment with colored background. I have defined it in two ways:

\documentclass{article}

\usepackage[x11names]{xcolor}
\usepackage{adjustbox}
\newsavebox{\savedbox}

\newenvironment{colbox1}[1]
    {
    \begin{adjustbox}{minipage=[c]{\textwidth},margin= 5mm 5mm 5mm 5mm, bgcolor=#1,env=center}
    }
    {
    \end{adjustbox}
    }


\newsavebox{\savedbox}
\newenvironment{colbox2}[1]
    {
    \newcommand\colboxcolor{#1}
    \begin{lrbox}{\savedbox}
    \begin{minipage}[c]{\textwidth}
    }
    {
    \end{minipage}
    \end{lrbox}
    \begin{center}
      \colorbox{\colboxcolor}{\usebox{\savedbox}}
    \end{center}
    }

\begin{document}
Praesent fermentum erat rhoncus commodo feugiat. Etiam ut metus quis ex vehicula pretium. Aliquam erat volutpat. Morbi blandit justo a ex imperdiet finibus. Sed libero urna, efficitur vitae eros sagittis, pulvinar tempor eros. Fusce fermentum eget ante vitae auctor. Donec vitae ante accumsan, pretium urna id, lacinia nunc. Aenean augue felis, pretium eget nisi non, bibendum hendrerit urna. Aenean aliquet tempor augue laoreet posuere. Pellentesque vitae est faucibus, sollicitudin odio in, elementum enim. Maecenas ornare lorem in nunc semper, at malesuada neque eleifend. Sed non ante tempus, congue nibh ac, finibus ipsum. Nunc quis pretium elit, a faucibus metus.

\begin{colbox1}{Cornsilk2}
    Pellentesque vitae est faucibus, sollicitudin odio in, elementum enim. Maecenas ornare lorem in nunc semper, at malesuada neque eleifend.
\end{colbox1}

Aliquam erat volutpat. Morbi blandit justo a ex imperdiet finibus. Sed libero urna, efficitur vitae eros sagittis, pulvinar tempor eros. Fusce fermentum eget ante vitae auctor. Donec vitae ante accumsan, pretium urna id, lacinia nunc. Aenean augue felis, pretium eget nisi non.

\begin{colbox2}{Cornsilk2}
    Pellentesque vitae est faucibus, sollicitudin odio in, elementum enim. Maecenas ornare lorem in nunc semper, at malesuada neque eleifend.
\end{colbox2}

\end{document}

I would like the box to have margin and fit the text width. In colbox1, if I write 0.8\textwidth , I get a box that isn't larger than the text width. However I would like to make the colored background to have exactly the same width of the text. (If someone knows a specific way to create such boxes, I would also love to know.)

See this print image: pdflatex output

  • 1
    Check the tcolorbox-package. – Ulrike Fischer May 6 '17 at 19:47
3

Using tcolorbox without frame and sharp corners (no. 2 with adjustable margins):

boxes

\documentclass{article}

\usepackage[x11names]{xcolor}
\usepackage[most]{tcolorbox}

\newtcolorbox{colbox}[1]{colback=#1,colframe=#1,sharp corners}
\newtcolorbox{colbox2}[1]{colback=#1,colframe=#1,left=5mm,right=5mm,top=5mm,bottom=5mm,sharp corners}

\begin{document}
Praesent fermentum erat rhoncus commodo feugiat. Etiam ut metus quis ex vehicula pretium. Aliquam erat volutpat. Morbi blandit justo a ex imperdiet finibus. Sed libero urna, efficitur vitae eros sagittis, pulvinar tempor eros. Fusce fermentum eget ante vitae auctor. Donec vitae ante accumsan, pretium urna id, lacinia nunc. Aenean augue felis, pretium eget nisi non, bibendum hendrerit urna. Aenean aliquet tempor augue laoreet posuere. Pellentesque vitae est faucibus, sollicitudin odio in, elementum enim. Maecenas ornare lorem in nunc semper, at malesuada neque eleifend. Sed non ante tempus, congue nibh ac, finibus ipsum. Nunc quis pretium elit, a faucibus metus.

\begin{colbox}{Cornsilk2}
    Pellentesque vitae est faucibus, sollicitudin odio in, elementum enim. Maecenas ornare lorem in nunc semper, at malesuada neque eleifend.
\end{colbox}

Aliquam erat volutpat. Morbi blandit justo a ex imperdiet finibus. Sed libero urna, efficitur vitae eros sagittis, pulvinar tempor eros. Fusce fermentum eget ante vitae auctor. Donec vitae ante accumsan, pretium urna id, lacinia nunc. Aenean augue felis, pretium eget nisi non.

\begin{colbox2}{Cornsilk2}
    Pellentesque vitae est faucibus, sollicitudin odio in, elementum enim. Maecenas ornare lorem in nunc semper, at malesuada neque eleifend.
\end{colbox2}

\end{document}
  • @Bobyandbob I don't exactly know which... – TeXnician May 6 '17 at 20:24
  • A inner margin between box and (box-)text. Like margin= 5mm 5mm 5mm 5mm for adjustbox. – Bobyandbob May 6 '17 at 20:31
  • 1
    @Bobyandbob Now with adjustable margins. But I understood that the OP just desperately tried to get a box with some margin (like my first, not that tight as his second) which fits textwidth. – TeXnician May 6 '17 at 20:35
0

If I understand you correctly, then you only need to reduce the minipage width by the left and right margin, i.e. use \textwidth-10mm instead of \textwidth.

\documentclass{article}

\usepackage[x11names]{xcolor}
\usepackage{lipsum}% for example text
\usepackage{adjustbox}

\newenvironment{colbox1}[1]
    {%
    \begin{adjustbox}{minipage=[c]{\textwidth-10mm},margin= 5mm 5mm 5mm 5mm, bgcolor={#1},env=center}%
    }
    {%
    \end{adjustbox}%
    }

\begin{document}
\lipsum[1]
\begin{colbox1}{Cornsilk2}
    Pellentesque vitae est faucibus, sollicitudin odio in, elementum enim. Maecenas ornare lorem in nunc semper, at malesuada neque eleifend.
\end{colbox1}
\lipsum[2]

\end{document}

Screenshot

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.