Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

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):

Too wide box

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

Thanks.

share|improve this question
2  
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
add comment

2 Answers

up vote 5 down vote accepted

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.

share|improve this answer
add comment

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}
\addtolength{\mycolwidth}{-2\tabcolsep}

\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}
share|improve this answer
1  
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
    
@Martin:Thanks, I've adjusted my answer. –  Yossi Farjoun Feb 23 '11 at 14:04
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.