# Problem with box width

I have the following code, that draws a dashed box:

\documentclass{book}

\usepackage{lipsum}

\usepackage{arydshln,paralist}

\setlength{\parindent}{0pt}

\newenvironment{mybox}[1]
{\vspace{6pt}\par\tabular{:p{\linewidth}:}\hdashline
\rule{0pt}{4ex}#1\\
\compactitem}
{\endcompactitem\hspace*{1ex}\\\hdashline\endtabular\vspace*{1ex}}

\begin{document}
\lipsum[1]

\begin{mybox}{There are many items:}
\item First item
\item Second item
\end{mybox}

\end{document}


The results are shown below (notice that the box is wider than it should):

However, it gets drawn too wide, not respecting my margins. How do I change the defined box environment to reduce it's width?

Thanks.

• I think that the problem is that you are forgetting to account for the intercolumn width. I don't remember what the default is, but try removing it first with a :@{}p{...}@{}: and see if that helps. – Yossi Farjoun Feb 23 '11 at 13:10

As already mentioned you have to remove twice the amount of the tabular column separation(?) \tabcolsep from the length. This can be easily done using \dimexpr (eTeX) as shown below. Alternativly you can use a length register as in Yossi's answer.

\documentclass{book}
\usepackage{lipsum}

\usepackage{arydshln,paralist}

\setlength{\parindent}{0pt}

\newenvironment{mybox}[1]
{\vspace{6pt}\par\tabular{:p{\dimexpr\linewidth-2\tabcolsep}:}\hdashline
\rule{0pt}{4ex}#1\\
\compactitem}
{\endcompactitem\hspace*{1ex}\\\hdashline\endtabular\vspace*{1ex}}

\begin{document}
\lipsum[1]

\begin{mybox}{There are many items:}
\item First item
\item Second item
\end{mybox}

\end{document}


Using @{} to set this sep value to zero doesn't work here properly. Around the : it causes an error and inside it makes the text touch the dashed line.

The problem is that the columns add space around the width that you specified. LaTeX is simply doing what you told it to do. For "correct" output, you'll need to decrease the width of the text by 2\tabcolsep:

\documentclass{book}

\usepackage{lipsum}
\usepackage{arydshln,paralist}

\setlength{\parindent}{0pt}
\newlength{\mycolwidth}
\setlength{\mycolwidth}{\linewidth}

\newenvironment{mybox}[1]
{\vspace{6pt}\par\tabular{:p{\mycolwidth}:}\hdashline
\rule{0pt}{4ex}#1\\
\compactitem}
{\endcompactitem\hspace*{1ex}\\\hdashline\endtabular\vspace*{1ex}}

\begin{document}
\lipsum[1]

\begin{mybox}{There are many items:}
\item First item
\item Second item
\end{mybox}

\end{document}

• I've made your code slightly more compact; hope that's OK. – Hendrik Vogt Feb 23 '11 at 13:29
• You are looking for \tabcolsep. – Martin Scharrer Feb 23 '11 at 13:30