GeneralColored Box in new Environment

LaTeX specific issues not fitting into one of the other forums of this category.
Post Reply
Selveste1
Posts: 2
Joined: Mon Aug 15, 2011 5:51 am

Colored Box in new Environment

Post by Selveste1 »

Hey everybody, 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.

Code: Select all

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

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

%DEFINE ENVIRONMENT BLOCK 
% Riddle
\newenvironment{Riddle}{		% Riddle environment
  \begin{center}			% Centering minipage
    \colorbox[HTML]{F8E0E0} {		% Set's the color of minipage
      \begin{minipage}[b]{380px} 	% Starts minipage
 	\textbf{Riddle: }\\ \itshape 	% 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{Riddle}
      some text here
    \end{Riddle}

\end{document}
colorbox.png
colorbox.png (9.56 KiB) Viewed 5960 times

Code: Select all

% Riddle
\newenvironment{Riddle}{		% Riddle environment
  \begin{center}			% Centering minipage
    \colorbox[HTML]{F8E0E0} {		% Set's the color of minipage
      \begin{minipage}[b]{380px} 	% Starts minipage
 	\textbf{Riddle: }\\ \itshape 	% Set's title and starts italic for text 
      \end{minipage}}			% End minipage
}{\end{center}}				% End Riddle environment
As I can see it, the problem is that in my environment colorbox is not ended by the end parameter, is there a way to do this right.?

Recommended reading 2024:

LaTeXguide.org • LaTeX-Cookbook.net • TikZ.org

NEW: TikZ book now 40% off at Amazon.com for a short time.

Stefan Kottwitz
Site Admin
Posts: 10348
Joined: Mon Mar 10, 2008 9:44 pm

Colored Box in new Environment

Post by Stefan Kottwitz »

Hi,

I guess that can be made easier with the mdframed package.

Stefan
LaTeX.org admin
Selveste1
Posts: 2
Joined: Mon Aug 15, 2011 5:51 am

Re: Colored Box in new Environment

Post by Selveste1 »

mdframed looks as a nice second option, but I would prefer to keep it in the standard libraries if possible

/Henrik
User avatar
Stefan Kottwitz
Site Admin
Posts: 10348
Joined: Mon Mar 10, 2008 9:44 pm

Re: Colored Box in new Environment

Post by Stefan Kottwitz »

Hi Henrik,

mdframed is an improved version of the framed package, which is a classic package contained in all LaTeX distributions I know. You can also use framed instead.

If you restrict yourself to the standard classes, you cannot benefit from hundreds of classes and packages created by skilled TeX programmers. This is a power of LaTeX. Of cause you can program all you need yourself if you worry about compilation speed or document size, for example.

At least I don't like to spend much time on re-programming already existing and documented package features. I hope it helps if I point to it. You could also learn to write such colored box environments by reading the package code.

Stefan
LaTeX.org admin
Post Reply