3

I would like to horizontally offset a framed paragraph of text so that the sides of the frame coincide precisely with left and right margins of 2em on a page with a text-width of 6.5 inches.

Indenting a framed parbox or minipage is problematic because the required width is subject to a conflict of units between the margins, the text-width, and the inherent fboxsep:

\hspace{2em}\fbox{\parbox{?}{Paragraph text}}

What would you suggest?

3

Here are two options that provide similar layout. framedparA uses a center environment to set the contents horizontally inside a \parbox of required width. framedparB uses mdframed and allows to be broken across the page boundary.

enter image description here

\documentclass{article}

\usepackage{lipsum,environ,mdframed}

\NewEnviron{framedparA}[1][2em]{%
  \begin{center}
  \fbox{%
    \parbox{\dimexpr\linewidth-#1-#1-2\fboxsep-2\fboxrule}{%
      \strut\BODY\strut%
    }%
  }%
  \end{center}
}

\NewEnviron{framedparB}[1][2em]{%
  \begin{mdframed}[
    leftmargin=#1,
    rightmargin=#1,
    innerleftmargin=\fboxsep,
    innerrightmargin=\fboxsep
  ]
    \BODY
  \end{mdframed}
}

\begin{document}

Lorem ipsum dolor sit amet, consectetur adipiscing elit. Vivamus molestie dui tellus, 
vitae blandit quam scelerisque in. Ut tristique metus id magna commodo, nec semper sapien 
efficitur. Sed pretium elit vitae mi tristique mollis. Nam nibh metus, dictum et turpis 
sit amet, tincidunt vulputate sem.

\begin{framedparA}
Vestibulum ante ipsum primis in faucibus orci luctus et ultrices posuere cubilia Curae; 
Etiam vel euismod massa, quis fermentum nisl. Aenean pellentesque tincidunt nibh, in 
ornare mi malesuada ut. Sed imperdiet enim leo, at aliquam dolor finibus a. Duis odio 
mi, molestie sed tortor efficitur, blandit molestie purus.
\end{framedparA}

Proin auctor risus et dui lobortis ultrices. Morbi et mauris id elit fringilla posuere. 
Aenean laoreet lacus est, eu congue purus egestas nec. Sed non velit nulla. Cras vestibulum 
ut massa quis commodo. Sed tempor, lorem a porta dictum, orci nulla scelerisque arcu, sed 
lacinia nisi risus interdum velit. Duis nec erat ac urna pellentesque imperdiet.

\begin{framedparB}
Vestibulum ante ipsum primis in faucibus orci luctus et ultrices posuere cubilia Curae; 
Etiam vel euismod massa, quis fermentum nisl. Aenean pellentesque tincidunt nibh, in 
ornare mi malesuada ut. Sed imperdiet enim leo, at aliquam dolor finibus a. Duis odio 
mi, molestie sed tortor efficitur, blandit molestie purus.
\end{framedparB}

Proin auctor risus et dui lobortis ultrices. Morbi et mauris id elit fringilla posuere. 
Aenean laoreet lacus est, eu congue purus egestas nec. Sed non velit nulla. Cras vestibulum 
ut massa quis commodo. Sed tempor, lorem a porta dictum, orci nulla scelerisque arcu, sed 
lacinia nisi risus interdum velit. Duis nec erat ac urna pellentesque imperdiet.

\end{document}

Of course, with mdframed there's far more fancy stuff that can be added...

  • It was not easy choosing between these three excellent answers - David, Werner, and egreg. You guys are the best. Ultimately, I went with Werner's environment approach because I'll be applying this framed-box in multiple instances across a collection of documents. But now egreg has me thinking about color.... Thank you, all. – steven_nevets Jan 24 '15 at 0:51
  • BTW, it's the \dimexpr that lies at the heart of these solutions and is precisely what I was missing. I've got some research to do. – steven_nevets Jan 24 '15 at 0:54
  • @steven_nevets: Look at the mdframed documentation - it provides similar page-breakable functionality to tcolorbox with all the graphic support provided by tikz and/or pstricks... – Werner Jan 24 '15 at 1:16
5

Your question could be clearer but I think you want

\noindent
\hspace*{2em}%
\fbox{\parbox{\dimexpr\textwidth-4em-2\fboxsep-2\fboxrule\relax}%
{Paragraph text}}

Or perhaps simpler

\begin{center}
 \fbox{\parbox{\dimexpr\textwidth-4em-2\fboxsep-2\fboxrule\relax}%
  {Paragraph text}}
\end{center}
4

You can use tcolorbox:

\documentclass{article}
\usepackage{tcolorbox}

\usepackage{lipsum}

\newlength{\boxindent}
\AtBeginDocument{\setlength{\boxindent}{2em}}

\begin{document}

%% A ``ruler''
\noindent
\vrule width 2em height 0.4pt depth 0pt
\kern-.2pt
\vrule width .4pt height 3pt depth 2.6pt
\kern-.2pt
\vrule width \dimexpr\textwidth-2em\relax height 0.4pt depth 0pt

\begin{tcolorbox}[
  left skip=\boxindent,
  right skip=0pt,
  width=\dimexpr\textwidth-\boxindent\relax
]
\lipsum[2]
\end{tcolorbox}

\end{document}

You can of course modify all the aspects of the box and it's probably better to define a new environment:

\documentclass{article}
\usepackage{tcolorbox}

\usepackage{lipsum}

\newlength{\boxindent}
\AtBeginDocument{\setlength{\boxindent}{2em}}

\newtcolorbox{mybox}{
  left skip=\boxindent,
  right skip=0pt,
  width=\dimexpr\textwidth-\boxindent\relax,
  boxrule=0.4pt,
  colframe=red!75!black,
  colback=red!5!white,
  sharp corners,
}

\begin{document}

%% A ``ruler''
\noindent
\vrule width 2em height 0.4pt depth 0pt
\kern-.2pt
\vrule width .4pt height 3pt depth 2.6pt
\kern-.2pt
\vrule width \dimexpr\textwidth-2em\relax height 0.4pt depth 0pt

\begin{tcolorbox}[
  left skip=\boxindent,
  right skip=0pt,
  width=\dimexpr\textwidth-\boxindent\relax
]
\lipsum[2]
\end{tcolorbox}

\bigskip

\begin{mybox}
\lipsum[2]
\end{mybox}

\end{document}

enter image description here

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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