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}
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,
Best Answer
The box has to be collected before typesetting it with a colored background:
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
will work. Notice how one can carry the argument to the "end part" of the environment, this is a standard technique.