0

Let's say we want to get one or two paragraphs of text into a visible framed such that:

  • the total width of the frame (including its borders) is the exactly same as the width of the surrounding text (in particular, the frame should NOT protrude into the right margin)

AND SIMULTANEOUSLY

  • the frame stays on one page, i.e., the frame doesn't span multiple pages.

Here are my unsuccessful attempts so far:

\documentclass{article}
\usepackage{lipsum,framed}
\begin{document}
\vspace*{72ex}
\lipsum[1]
\begin{framed}%% NOT ON ONE PAGE!
  \begin{samepage}%
    \textbf{Some Title:}\par
    \lipsum[2]%
  \end{samepage}%
\end{framed}
\lipsum[3]
\par\medskip\noindent\fbox{\parbox{\textwidth}{%% GOES INTO THE RIGHT MARGIN; I dunno how to compute the width properly!
  \indent\textbf{Some Title:}\par\indent%% first-line indents don't work!
  \lipsum[4]
}}
\end{document}

yields

bad output

As you see, the upper frame spans over the page break, whereas the lower frame occupies more horizontal space than the usual text. (Moreover, unfortunately, the first-line indents don't work in the lower frame. However, this is not that bad; I can work around it.)

What to do?

EDIT: Based on John's suggestion, I've tried this:

\documentclass{article}
\usepackage{lipsum,framed}
\begin{document}
\vspace*{72ex}
\lipsum[1]
\par\addvspace{\topsep}\noindent
{\fboxsep=3\fboxsep
\fbox{\parbox{\dimexpr\textwidth-2\fboxsep-2\fboxrule}{%
  \textbf{Some Title:}\\
  \lipsum[2]
}}}\par\addvspace{\topsep}\indent
\lipsum[3]
\end{document}

It does the job:

good output

However, the string of low-level commands is rather cumbersome and prone to error. Isn't there some command in some package that does this?

2
  • The framed package is designed to hand page breaks (which is harder)..All you need is an unbreakable box inside an \fbox, such as \parbox{dimexpr \textwidth-2\fboxsep-2\fboxrule}{...} Commented May 24, 2023 at 3:29
  • @JohnKormylo Do you mean, perhaps, \parbox{\dimexpr \textwidth-2\fboxsep-2\fboxrule}{...}?
    – user292998
    Commented May 24, 2023 at 3:37

1 Answer 1

1

I would use package tcolorbox which more or less does by default, what you expect:

\documentclass{article}
\usepackage{lipsum}
\usepackage{tcolorbox}
\usepackage{showframe}% to visualize the text area
\begin{document}
\vspace*{72ex}
\lipsum[1]
\begin{tcolorbox}
  \subsubsection*{Some Title:}
  \lipsum[2]%
\end{tcolorbox}
\lipsum[3]
\end{document}

default with grey background, round corners and thick lines

Or if you like sharp corners, thinner lines of the frame and white background:

\documentclass{article}
\usepackage{lipsum}
\usepackage{tcolorbox}
%\usepackage{showframe}% to visualize the text area
\begin{document}
\vspace*{72ex}
\lipsum[1]
\begin{tcolorbox}[sharp corners=all,boxrule=\fboxrule,colback=white]
  \subsubsection*{Some Title:}
  \lipsum[2]%
\end{tcolorbox}
\lipsum[3]
\end{document}

sharp corners, thin lines, white background

If you also want these settings for all frames or additionally want to change the inner margin or other options, please see the manual for more information.

1
  • Works just fine, thx! Except that the DVI output (as opposed to, say, that of framed or mdframed) is not complete. The Postscript output is o.k. again.
    – user292998
    Commented May 24, 2023 at 18:49

You must log in to answer this question.