Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

I have a problem, I'm fairly new to LaTeX but I think I got the basics.

My problem is that wants to create a environment that can create a minipage with a background color and some text in it.

I can make this by doing it by hand, but I have a lot of boxes, and to write mostly the same over and over again is to my a bit redundant.

\documentclass[12pt,a4paper]{book}   % Book settings + layout

\usepackage{xcolor}         % Extended colors
\usepackage{color}         % Color extended names

%DEFINE ENVIRONMENT BLOCK
% Riddle
\newenvironment{colbox}[3]{        % Riddle environment
  \begin{center}                   % Centering minipage
    \colorbox[HTML]{#1} {          % Set's the color of minipage
      \begin{minipage}[b]{380px}   % Starts minipage
   \textbf{#2}\\ \textit{#3}       % Set's title and starts italic for text
  \end{minipage}}                  % End minipage
}{\end{center}}                    % End Riddle environment


\begin{document}

\begin{center}
    \colorbox[HTML]{F8E0E0}{
     \begin{minipage}[c]{380px}
      \textbf{Riddle: }\\ \textit{some text here}
     \end{minipage}}
\end{center}

Some other text

\begin{colbox}{F8E0E0}{Riddle:}
  some text here
\end{colbox}

Some more text

\begin{colbox}{F8E0E0}{Riddle:}
  {some text here}
\end{colbox}

\end{document}

Picture of the problem

As I can see it, the problem is that in my environment colorbox is not ended by the end parameter, and I need to move \end{minipage} the same place, but I can't move it before the \end{colorbox} is moved, is there a way to do this right?

%DEFINE ENVIRONMENT BLOCK
% Riddle
\newenvironment{colbox}[3]{        % Riddle environment
  \begin{center}                   % Centering minipage
    \colorbox[HTML]{#1} {          % Set's the color of minipage
      \begin{minipage}[b]{380px}   % Starts minipage
   \textbf{#2}\\ \textit{#3}       % Set's title and starts italic for text
  \end{minipage} }                 % End minipage
}{\end{center}}

I can "fix" the out-of-box problem by putting {} on the text like this

\begin{colbox}{F8E0E0}{Riddle:}
  {some text here}
\end{colbox}

But that's not really what I want.

If it's possible I would like to keep everything in what's default in LaTeX cause I'm not the only one maintaining this document and special packages is not well liked,

share|improve this question
    
Just a comment on the use of xcolor + color packages: xcolor already has options to get more names, for example \usepackage[svgnames]{xcolor} makes a lot of names available. See the xcolor manual. –  ℝaphink Sep 28 '11 at 15:41
    
The question tex.stackexchange.com/questions/29060/newenvironment-issues seems to be very similar, but has an extra comment-it-out part. –  Martin Scharrer Sep 28 '11 at 16:12
add comment

4 Answers

up vote 11 down vote accepted

The box has to be collected before typesetting it with a colored background:

\newsavebox{\selvestebox}
\newenvironment{colbox}[1]
  {\newcommand\colboxcolor{#1}%
   \begin{lrbox}{\selvestebox}%
   \begin{minipage}{\dimexpr\columnwidth-2\fboxsep\relax}}
  {\end{minipage}\end{lrbox}%
   \begin{center}
   \colorbox[HTML]{\colboxcolor}{\usebox{\selvestebox}}
   \end{center}}

The lrbox environment is very useful for this kind of business.

You don't have to guess the width (and px is not the unit I would use); just remember that a padding of \fboxsep is added to the contents of \colorbox.

Now

\begin{colbox}{F8E0E0}
\textbf{Riddle: }\\ \textit{some text here}
\end{colbox}

will work. Notice how one can carry the argument to the "end part" of the environment, this is a standard technique.

share|improve this answer
    
Well clarified answer! –  Werner Sep 28 '11 at 15:33
    
Isn't that basically the same as tex.stackexchange.com/questions/29060/newenvironment-issues/…. Maybe the questions should be merged. –  Martin Scharrer Sep 28 '11 at 16:08
    
@MartinScharrer They are almost the same indeed. –  egreg Sep 28 '11 at 16:09
    
@egreg: Anyway, I decided that both questions only overlap but aren't complete duplicates. I now also added an answer using adjustbox. –  Martin Scharrer Sep 28 '11 at 16:23
add comment

To simplify the creation of such environments you can use a packages like framed or environ.

Example with framed (this allows page breaks):

\newenvironment{colbox}{%
   \def\FrameCommand{\colorbox{colboxcolor}}%
   \MakeFramed{\advance\hsize-\width \FrameRestore}}
 {\endMakeFramed}

and e.g.

\definecolor{colboxcolor}{HTML}{F8E0E0}

Then use:

\begin{colbox}
Text
\end{colbox}

Example with package environ (no page break):

\NewEnviron{colbox}[1][\linewidth]{%
  \colorbox{\colboxcolor}{%
     \begin{minipage}{#1}
       \BODY
     \end{minipage}%
  }
}
share|improve this answer
add comment

As stated in for very related question \newenvironment issues such boxes can be done quite easily with the adjustbox package. The technical solution is basically the same as in egreg's answer but with a much nicer and more flexible user interface. You can adjust the margin with the margin key and set the background color with bgcolor which uses similar code like \colorbox. You need to load xcolor by yourself for this.

\documentclass{article}

\usepackage{xcolor}
\usepackage{adjustbox}

\newenvironment{colbox}[2]{%
    \begin{adjustbox}{minipage=[b]{380px},margin=1ex,bgcolor=#1,env=center}% or use `bgcolor={HTML}{#1}` if you want to force HTML colors
        \textbf{#2}\\
}{%
    \end{adjustbox}%
}

\begin{document}

\begin{colbox}{green}{Riddle me this, Batman:}
    What is ....
\end{colbox}

\begin{colbox}{{HTML}{F8E0E0}}{Riddle 2:}
    What is ....
\end{colbox}

\end{document}

Result

share|improve this answer
add comment

Thank you all, for all your answers...

I looked at all of them, and have decided that the first solution is the right one for me.

The reasoning for this is: It's a very simple solution and it gets the job done, and as stated in the first post I'm not the only one maintaining this document, so simplicity is a good thing...

Again thanks for you help, and have a nice day to all

My Final Solution

If somebody in the future wants to see the answer I ended up with, have I supplied my final code:

\documentclass[12pt,a4paper]{book}   % Book settings + layout

\usepackage{xcolor}         % Extended colors
\usepackage{color}         % Color extended names

%DEFINE ENVIRONMENT BLOCK
% Riddle
\newsavebox{\riddlebox}
\newenvironment{colboxTre}[1]
{\newcommand\colboxcolor{#1}%
\begin{lrbox}{\riddlebox}%
\begin{minipage}{\dimexpr\columnwidth-2\fboxsep\relax} \textbf{Riddle:} \\ \itshape }
{\end{minipage}\end{lrbox}%
\begin{center}
  \colorbox[HTML]{\colboxcolor}{\usebox{\riddlebox}}
\end{center}}


\begin{document}

\begin{colboxTre}{F8E0E0}
  some text here
\end{colboxTre}

Some more text

\end{document}

My Final Solution

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.