GeneralColor inside a minipage?

LaTeX specific issues not fitting into one of the other forums of this category.
User avatar
Stefan Kottwitz
Site Admin
Posts: 10347
Joined: Mon Mar 10, 2008 9:44 pm

Color inside a minipage?

Post by Stefan Kottwitz »

Hi Andrea,

using \newcommand for example:

Code: Select all

\newcommand\mybox[1]{%
  \begin{center}%
    \fcolorbox{black}{red}{%
      \begin{minipage}[t]{1.0\textwidth}%
        #1%
      \end{minipage}}%
  \end{center}}
Btw. using the calc package the width of the box could be reduced to match the textwidth exactly, at the moment it's a bit too wide because of the separation space and the rule thickness of the fcolorbox.

Stefan
LaTeX.org admin

Recommended reading 2024:

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

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

arota
Posts: 2
Joined: Wed Oct 08, 2008 1:30 pm

Re: Color inside a minipage?

Post by arota »

Thanks! It is exactly what I needed! 8-)

Btw, do you know where I can find a clear and complete tutorial (online) to learn how to better use the \newenvironment and \newcommand ?


Andrea
User avatar
Stefan Kottwitz
Site Admin
Posts: 10347
Joined: Mon Mar 10, 2008 9:44 pm

Color inside a minipage?

Post by Stefan Kottwitz »

Hi Andrea,

perhaps have look here: Introductions and Guides, there are some good tutorials listed that are freely available online. Follow the link to the TeX Catalogue to find more.

Specific parts that I recommend: Stefan
LaTeX.org admin
User avatar
Juanjo
Posts: 657
Joined: Sat Jan 27, 2007 12:46 am

Color inside a minipage?

Post by Juanjo »

Andrea,
In the case you are still interested in an environment version of \mybox, the following code may be useful:

Code: Select all

\documentclass[a4paper]{article}
\usepackage[margin=2cm]{geometry}
\usepackage{lipsum,framed,color}

\definecolor{shadecolor}{rgb}{1,0.8,0.3}
\newlength{\boxwidth}
\newsavebox{\boxcontainer}

% Improved version of Stephan_K's \mybox
% Needs: shadecolor, \boxwidth
\newcommand\mybox[1]{%
  \setlength{\boxwidth}{\textwidth}
  \addtolength{\boxwidth}{-2\fboxsep}
  \addtolength{\boxwidth}{-2\fboxrule}
  \begin{center}%
    \fcolorbox{black}{shadecolor}{%
      \begin{minipage}[t]{\boxwidth}%
        #1%
      \end{minipage}}%
  \end{center}}

% First environment version
% Needs: shadecolor, \boxwidth, \boxcontainer
\newenvironment{Mybox}%
  {\setlength{\boxwidth}{\textwidth}
   \addtolength{\boxwidth}{-2\fboxsep}
   \addtolength{\boxwidth}{-2\fboxrule}
   \begin{lrbox}{\boxcontainer}%
     \begin{minipage}{\boxwidth}}%
  {\end{minipage}\end{lrbox}%
   \begin{center}%
     \fcolorbox{black}{shadecolor}{\usebox{\boxcontainer}}
   \end{center}}

% Second environment version
% Needs: shadecolor, \boxwidth and the framed package
\newenvironment{MyBox}{%
  \def\FrameCommand{\fboxsep=\FrameSep \fboxrule=\FrameRule \fcolorbox{black}{shadecolor}}%
  \MakeFramed {\setlength{\boxwidth}{\textwidth}
  \addtolength{\boxwidth}{-2\FrameSep}
  \addtolength{\boxwidth}{-2\FrameRule}
  \setlength{\hsize}{\boxwidth} \FrameRestore}}%
 {\endMakeFramed}

\begin{document}

% Settings for \mybox and Mybox
\setlength{\fboxrule}{1pt}
\setlength{\fboxsep}{9pt}

% Additional settings for MyBox
\setlength{\FrameRule}{\fboxrule}
\setlength{\FrameSep}{\fboxsep}

% Text not framed
\lipsum[1]

% Text framed with \mybox
\mybox{\lipsum[1]}

% Text framed with Mybox
\begin{Mybox}
  \lipsum[1]
\end{Mybox}

% Text framed with MyBox
\begin{MyBox}
  \lipsum[1]
\end{MyBox}

% Text framed with MyBox. It can be splitted in two different (or more) pages
\begin{MyBox}
  \lipsum[1-5]
\end{MyBox}

\end{document}
In this code you can see an improved version of the \mybox command given by Stephan_K, so the framed box perfectly fits the text width. There also two environments named Mybox and MyBox. For short texts, \mybox, Mybox and MyBox yield the same result. However, for long texts, MyBox can be splitted along several pages. This is an obvious advantage over \mybox and Mybox. The (small, I think) price to pay is the loading of another package (framed) and the use of two additional parameters, \FrameSep and \FrameRule, used by the framed package as equivalents of \fboxsep and \fboxrule. Now, the choice is yours.
The CTAN lion is an artwork by Duane Bibby. Courtesy of www.ctan.org.
User avatar
Stefan Kottwitz
Site Admin
Posts: 10347
Joined: Mon Mar 10, 2008 9:44 pm

Color inside a minipage?

Post by Stefan Kottwitz »

Hi,

using the calc package would make it shorter, for instance the second environment:

Code: Select all

\newenvironment{MyBox}{%
  \def\FrameCommand{\fboxsep=\FrameSep \fboxrule=\FrameRule \fcolorbox{black}{shadecolor}}%
  \MakeFramed {\setlength{\hsize}{\textwidth-2\FrameSep-2\FrameRule} \FrameRestore}}%
{\endMakeFramed}
or perhaps without using \FrameSep, \FrameRule if it's not needed:

Code: Select all

\newenvironment{MyBox}{%
  \def\FrameCommand{\fcolorbox{black}{shadecolor}}%
  \MakeFramed{\setlength{\hsize}{\textwidth-2\fboxsep-2\fboxrule} \FrameRestore}}%
{\endMakeFramed}
Stefan
LaTeX.org admin
User avatar
Juanjo
Posts: 657
Joined: Sat Jan 27, 2007 12:46 am

Color inside a minipage?

Post by Juanjo »

Searching for a different matter, I've realized that many of the features provided by the calc package can be now obtained through the \dimexpr and \numexpr primitives, new in e-TeX. Taking into account the last suggestions of Stephan_K, the macro and environments \mybox, Mybox and MyBox can be defined as follows:

Code: Select all

\newcommand\mybox[1]{%
  \begin{center}%
    \fcolorbox{black}{shadecolor}{%
      \begin{minipage}[t]{\dimexpr\textwidth-2\fboxsep-2\fboxrule}%
        #1%
      \end{minipage}}%
  \end{center}}

\newenvironment{Mybox}%
  {\begin{lrbox}{\boxcontainer}%
     \begin{minipage}{\dimexpr\textwidth-2\fboxsep-2\fboxrule}}%
  {\end{minipage}\end{lrbox}%
   \begin{center}%
     \fcolorbox{black}{shadecolor}{\usebox{\boxcontainer}}
   \end{center}}

\newenvironment{MyBox}{%
  \def\FrameCommand{\fcolorbox{black}{shadecolor}}%
  \MakeFramed{\setlength{\hsize}{\dimexpr\textwidth-2\fboxsep-2\fboxrule} \FrameRestore}}%
 {\endMakeFramed}
This code should work in any modern TeX system.
The CTAN lion is an artwork by Duane Bibby. Courtesy of www.ctan.org.
Post Reply