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'm trying to put a border around multiple lines. Here's a MWE with what I've achieved so far:

\documentclass[a4paper]{scrartcl}
\usepackage{calc}

\begin{document}
\setlength{\parindent}{0cm}
text text text

\fbox{
  \begin{minipage}[t]{\textwidth - 2\fboxsep}
    text in box
  \end{minipage}
}

more text
\end{document}

This results in something like this:

  text text text
  +-------------+
  | text in box |
  +-------------+
  more text

I would like the sideways borders to be around the type area:

  text text text
+-----------------+
| text in box     |
+-----------------+
  more text

But how can I do this?

share|improve this question

4 Answers 4

up vote 5 down vote accepted

The idea is to typeset a minipage and frame it; however we have to push it a bit leftwards and avoid overfull boxes; here's a way:

\documentclass{article}
\usepackage{lipsum}
\usepackage{xparse}

\newsavebox{\fminipagebox}
\NewDocumentEnvironment{fminipage}{m O{\fboxsep}}
 {\par\kern#2\noindent\begin{lrbox}{\fminipagebox}
  \begin{minipage}{#1}\ignorespaces}
 {\end{minipage}\end{lrbox}%
  \makebox[#1]{%
    \kern\dimexpr-\fboxsep-\fboxrule\relax
    \fbox{\usebox{\fminipagebox}}%
    \kern\dimexpr-\fboxsep-\fboxrule\relax
  }\par\kern#2
 }

\begin{document}

\lipsum[2]
\begin{fminipage}{\textwidth}
\lipsum[2]
\end{fminipage}
\lipsum[2]

\end{document}

You can also call it like

\begin{fminipage}{\textwidth}[1ex]

with any dimension you like, for stating the separation from the lines above and below (default is \fboxsep).

enter image description here

share|improve this answer
    
I noticed that if the text exactly fills one page, then this kicks the text and surrounding box onto a second page (leaving the first page blank). –  Joe Corneli Oct 6 '13 at 20:25
    
@JoeCorneli Yes, of course. In that case the box is too large and TeX does its best. –  egreg Oct 6 '13 at 20:46
    
This is not working for me; it may be to do with xparse, which is not on my version of MiKTeX 2.9. Is it something to do with LaTeX3 vs LaTeX2e, which I am using? –  user2868156 Jul 27 at 12:06
    
@user2868156 Can you give some more information? –  egreg Jul 27 at 12:08
    
Yes! I get the error File xparse.sty not found. when compiling. I also tried downloading xparse.sty itself and the l3packages.zip files, but to no avail. Is MiKTeX just not recognizing it? –  user2868156 Jul 27 at 12:15

You can use a negative space to reset the indentation

\documentclass[a4paper]{scrartcl}
\usepackage{calc}

\begin{document}
\setlength{\parindent}{0cm}
text text text

\hspace{\dimexpr-\fboxrule-\fboxsep\relax}\fbox{%
  \begin{minipage}[t]{\widthof{text in box}}
    text in box
  \end{minipage}%
}

more text
\end{document}

Also you need to remove the spurious space with % after \fbox.

enter image description here

share|improve this answer

If i understand you correctly then you want to \hskip/\hspace* left by 2\fboxsep and \fboxrule.

\documentclass[a4paper]{scrartcl}
\usepackage{calc}

\begin{document}
\setlength{\parindent}{0cm}
text text text

\hspace*{\dimexpr-2\fboxsep-\fboxrule} \fbox{
  \begin{minipage}[t]{\textwidth - 2\fboxsep}
   text in box
  \end{minipage}
  }

  more text
  \end{document}
share|improve this answer

Another try:

\documentclass[a4paper]{scrartcl}
\usepackage{calc}

\begin{document}
\setlength{\parindent}{0cm}
text text text

\hspace*{-2.5mm}%
\fbox{%
  \hspace*{2.5mm}\hspace*{-1\fboxsep}%
  \parbox{\textwidth + 5mm - 2\fboxsep}{%
  \begin{minipage}[t]{\textwidth - 2\fboxsep}
    text in box
  \end{minipage}
  }%
}

more text more text more text more text more text more text more text more text more text more text more text more text more text more text more text more text more text more text more text more text more text more text more text more text more text more text more text more text 
\end{document}

enter image description here

share|improve this answer

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.