Text FormattingColored box size and alignment

Information and discussion about LaTeX's general text formatting features (e.g. bold, italic, enumerations, ...)
Post Reply
jaybz
Posts: 90
Joined: Sun Jul 11, 2010 6:02 pm

Colored box size and alignment

Post by jaybz »

How can I get the colored box to be the width of the text "Here we we have sample text for..."

Code: Select all

\documentclass[a4paper]{article}
\usepackage[top=14mm, bottom=14mm, left=28mm, right=28mm]{geometry}
\usepackage{lipsum,framed,color}

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

\newenvironment{xlist}{%
  \begin{list}{}{%
    \setlength{\labelwidth}{5em}
    \setlength{\labelsep}{1em}
    \setlength{\leftmargin}{\labelwidth}
    \addtolength{\leftmargin}{\labelsep}
    \setlength{\rightmargin}{0pt}
    \setlength{\parsep}{0.5ex plus 0.2ex minus 0.1ex}
    \setlength{\itemsep}{0.3ex plus 0.2ex}
    \renewcommand{\makelabel}[1]{\textsf{##1}\hfil}
  }
}
{\end{list}}
    
\newcommand\mybox[1]{%
  %\begin{center}%
    \fcolorbox{black}{shadecolor}{%
      \begin{minipage}[t]{\dimexpr\textwidth\fboxsep+10\fboxrule}%
        #1%
      \end{minipage}}%
  %\end{center}
  }    

\begin{document}

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

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

\begin{xlist}
\item[Solution 1]Here we we have sample text for this document to fill up space that says nothing at all in particular and just runs on filling space.\\[4mm]
\end{xlist}

\begin{xlist}
\item[Solution 2]Here we more have sample text for this document to fill up space that says nothing at all in particular and just runs on filling space.\\[4mm]
\end{xlist}

\mybox{
\textbf{Theorem 3.2}
\begin{center}
$\displaystyle x+\sin(x)$ Blah blah blah blah blah blah blah blah blah blah blah blah blah
\end{center}
}
\end{document}
Last edited by jaybz on Sat May 07, 2011 2:27 am, edited 1 time in total.

Recommended reading 2024:

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

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

5gon12eder
Posts: 126
Joined: Sun Feb 13, 2011 8:36 pm

Colored box size and alignment

Post by 5gon12eder »

There are two problems:

1. You didn't calculate the width of the minipage correctly. Using the calc package, it should be something like

Code: Select all

\begin{minipage}[t]{\textwidth-2\fboxsep}
2. Your box is indented. Hence, even if it had textwidth, it would be shifted to the right and still won't be ligned up with the text body. The solution is to put a \noindent before it.

Best
I'm using pdfTeX, Version 3.1415926-1.40.10 (TeX Live 2009/Debian).
jaybz
Posts: 90
Joined: Sun Jul 11, 2010 6:02 pm

Colored box size and alignment

Post by jaybz »

Is this what you mean? If it is, it still doesn't work. I just get a box that's almost the width of the page. It should be the width of the line to the right of "Solution 2"

Code: Select all

    \documentclass[a4paper]{article}
    \usepackage[top=14mm, bottom=14mm, left=28mm, right=28mm]{geometry}
    \usepackage{lipsum,framed,color}
    \usepackage{calc}
    \definecolor{shadecolor}{rgb}{1,0.8,0.3}
    \newlength{\boxwidth}
    \newsavebox{\boxcontainer}

    \newenvironment{xlist}{%
      \begin{list}{}{%
        \setlength{\labelwidth}{5em}
        \setlength{\labelsep}{1em}
        \setlength{\leftmargin}{\labelwidth}
        \addtolength{\leftmargin}{\labelsep}
        \setlength{\rightmargin}{0pt}
        \setlength{\parsep}{0.5ex plus 0.2ex minus 0.1ex}
        \setlength{\itemsep}{0.3ex plus 0.2ex}
        \renewcommand{\makelabel}[1]{\textsf{##1}\hfil}
      }
    }
    {\end{list}}
       
    \newcommand\mybox[1]{%
      %\begin{center}%
        \fcolorbox{black}{shadecolor}{%
          %\begin{minipage}[t]{\dimexpr\textwidth-2\fboxsep+10\fboxrule}%
           \begin{minipage}[t]{\textwidth-2\fboxsep}
            #1%
          \end{minipage}}%
      %\end{center}
      }   

    \begin{document}

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

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

    \begin{xlist}
    \item[Solution 1]Here we we have sample text for this document to fill up space that says nothing at all in particular and just runs on filling space.\\[4mm]
    \end{xlist}

    \begin{xlist}
    \item[Solution 2]Here we more have sample text for this document to fill up space that says nothing at all in particular and just runs on filling space.\\[4mm]
    \end{xlist}

    \noindent
    \mybox{
    \textbf{Theorem 3.2}
    \begin{center}
    $\displaystyle  x+\sin(x)$ Blah blah blah blah blah blah blah blah blah blah blah blah blah
    \end{center}
    }
    \end{document}

5gon12eder
Posts: 126
Joined: Sun Feb 13, 2011 8:36 pm

Colored box size and alignment

Post by 5gon12eder »

If I run pdflatex on the code you've posted, I get the PDF in the attachment. To my understanding, that box has the width of the text. If you want something different, please specify more clearly.

Best
Attachments
document2.pdf
(44.52 KiB) Downloaded 489 times
I'm using pdfTeX, Version 3.1415926-1.40.10 (TeX Live 2009/Debian).
jaybz
Posts: 90
Joined: Sun Jul 11, 2010 6:02 pm

Re: Colored box size and alignment

Post by jaybz »

The right side of the box should be up against the right margin, which it is but the left side of the box isn't correct. Next to the "Solution 2" there is the line:
"all in particular and just runs on filling space."
The left side of the box should be under the "a" in "all"
5gon12eder
Posts: 126
Joined: Sun Feb 13, 2011 8:36 pm

Colored box size and alignment

Post by 5gon12eder »

So you actually want the box narrower than textwidth. It's not a big deal. Just subtract \leftmargin (6.0em in your case) from the width of the box and put

Code: Select all

\noindent\hspace{6.0em}
at the beginning of the \mybox definition.

It would be of course a lot nicer to define a new length instead of specifying it explicitly so everything will scale smoothly, once you decide to change indentation of your list environment.
I'm using pdfTeX, Version 3.1415926-1.40.10 (TeX Live 2009/Debian).
jaybz
Posts: 90
Joined: Sun Jul 11, 2010 6:02 pm

Colored box size and alignment

Post by jaybz »

The minipage wouldn't take a width of

Code: Select all

\textwidth-8em
I had to use just 8. So here it is working. Thank you for you're help.

Code: Select all

 \documentclass[a4paper]{article}
        \usepackage[top=14mm, bottom=14mm, left=28mm, right=28mm]{geometry}
        \usepackage{lipsum,framed,color}
        \usepackage{calc}
        \definecolor{shadecolor}{rgb}{1,0.8,0.3}
        \newlength{\boxwidth}
        \newsavebox{\boxcontainer}

        \newenvironment{xlist}{%
          \begin{list}{}{%
            \setlength{\labelwidth}{5em}
            \setlength{\labelsep}{1em}
            \setlength{\leftmargin}{\labelwidth}
            \addtolength{\leftmargin}{\labelsep}
            \setlength{\rightmargin}{0pt}
            \setlength{\parsep}{0.5ex plus 0.2ex minus 0.1ex}
            \setlength{\itemsep}{0.3ex plus 0.2ex}
            \renewcommand{\makelabel}[1]{\textsf{##1}\hfil}
          }
        }
        {\end{list}}
           
        \newcommand\mybox[1]{%
          %\begin{center}%
            \noindent\hspace{6.0em}\fcolorbox{black}{shadecolor}{%
              %\begin{minipage}[t]{\dimexpr\textwidth-2\fboxsep+10\fboxrule}%
               \begin{minipage}[t]{\textwidth-8\fboxsep}
                #1%
              \end{minipage}}%
          %\end{center}
          }   

        \begin{document}

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

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

        \begin{xlist}
        \item[Solution 1]Here we we have sample text for this document to fill up space that says nothing at all in particular and just runs on filling space.\\[4mm]
        \end{xlist}

        \begin{xlist}
        \item[Solution 2]Here we more have sample text for this document to fill up space that says nothing at all in particular and just runs on filling space.\\[4mm]
        \end{xlist}

        %\noindent
        \mybox{
        \textbf{Theorem 3.2}
        \begin{center}
        $\displaystyle  x+\sin(x)$ Blah blah blah blah blah blah blah blah blah blah blah blah blah
        \end{center}
        }
        \end{document}
Post Reply