12

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?

user23797
  • 123

4 Answers4

20

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

egreg
  • 1,121,712
6

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

percusse
  • 157,807
2

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}
Max
  • 5,702
1

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

NVaughan
  • 8,175