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 am looking for the correct way to lay out content in a framed box. In my opinion the space before the first section in a box looks odd in comparison to the same content at the start of a page. How can I remove the horizontal space before the first section in a framed box? In particular I am looking for a compositional solution, that is, I would like to use the same latex code no matter whether it is placed within a box or not.

\documentclass{article}

\setlength{\parindent}{0pt}

\setlength{\fboxrule}{2pt}

\usepackage{showframe}
\usepackage{blindtext}

\begin{document}

\section{Title}
\blindtext

\section{Title}
\blindtext


\fbox{\parbox{\dimexpr\linewidth-2\fboxsep-2\fboxrule}{%
\section{Title}
\blindtext

\section{Title}
\blindtext}}


\end{document}
share|improve this question

3 Answers 3

up vote 2 down vote accepted

Consider the following, where a minipage is used rather than a parbox in the third instance:

\documentclass{article}

\setlength{\parindent}{0pt}

\setlength{\fboxrule}{2pt}

\usepackage{showframe}
\usepackage{blindtext}

\begin{document}

\section{Title}
\blindtext

\section{Title}
\blindtext


\fbox{\parbox{\dimexpr\linewidth-2\fboxsep-2\fboxrule}{%
\section{Title}
\blindtext

\section{Title}
\blindtext}}

\fbox{%
\begin{minipage}{\dimexpr\linewidth-2\fboxsep-2\fboxrule}
\section{Title}
\blindtext

\section{Title}
\blindtext
\end{minipage}%
}
\end{document}

and which gives an output on the third page like:

output

share|improve this answer
    
oh and do note the % both in \fbox{% and at the end of the minipage where it necessary to type \end{minipage}% (or in fact \end{minipage}} if you prefer having the closing brace on the same line). –  greyshade Aug 11 at 7:46
    
and I hope you are aware that neither with a parbox nor with a minipage this will behave nicely at a page break.. –  greyshade Aug 11 at 7:51
    
Ah, I see, so I was actually looking for a minipage rather than a parbox, thanks. I am aware that the box is not broken. –  Jan Christiansen Aug 11 at 9:39

The usage of package tcolorbox provides, correctly setup, very tight boxes around the text and other nice features

Since two sections are involved, the box should be breakable.

The options left,top,right,bottom and boxsep control the spacing between the box frame and the box content.

\documentclass{article}

\usepackage{tcolorbox}
\tcbuselibrary{breakable}%

\usepackage{showframe}
\usepackage{blindtext}

\begin{document}

\section{Title}
\blindtext

\section{Title}
\blindtext

\begin{tcolorbox}[breakable,arc=0pt,auto outer arc,left=0pt,right=0pt,bottom=0pt,boxsep=0pt,top=0pt,width=\textwidth]%
\section{Title}
\blindtext

\section{Title}
\blindtext
\end{tcolorbox}%


\begin{tcolorbox}[breakable,arc=0pt,outer arc=0pt,left=0pt,right=0pt,bottom=0pt,boxsep=0pt,top=0pt,width=\textwidth,colback={yellow},boxrule=4pt]%
\section{Title}
\blindtext

\section{Title}
\blindtext
\end{tcolorbox}%



\end{document}

enter image description here

share|improve this answer
    
you forgot the breakable option in the specification of the tcolorbox - without it your solution has no advantage over the others. –  greyshade Aug 11 at 7:56
    
@greyshade: Yes, I noticed, that I have forgotten it, but it is not your job to edit my answers. While you were editing my answer, I was just adding it ;-) –  Christian Hupfer Aug 11 at 7:58
    
couldn't have known there was a bigger edit coming ;-) (and in the case of including \tcbuselibrary{breakable}, but then not using breakable I felt the author's intend was clear enough that an edit was warranted). –  greyshade Aug 11 at 8:03
    
@greyshade: A comment is often sufficient. And I personally refrain from editing other users' answers, except in the First Post queue, for formatting issues. –  Christian Hupfer Aug 11 at 8:06
    
Thanks for the solution, I was looking for a solution without using tcolorbox, I should probably have mentioned that. –  Jan Christiansen Aug 11 at 9:40

A solution with the framed package. To illustrate what can be done with it, I define a frshaded and a frshaded* environments, for which you can choose the colour of the frame and of the background. The difference is that the former environment has frame width equal to \linewidth, while the latter has frame built around a text of width \linewidth.

I adjusted the vertical spacing above first framed section with a \vspace*{-\baselineskip}, compensated by \vspace{\baselineskip} before the frame. That can easily be incorporated in a framedsections environment.

Here is the code:

    \documentclass{article}

    \setlength{\parindent}{0pt}
    \usepackage{etoolbox}
    \usepackage[x11names]{xcolor}
    \usepackage{showframe, framed}
    \usepackage{blindtext}

    \setlength\FrameRule{0.8pt}
    \setlength\FrameSep{3pt}
    \renewcommand{\FrameHeightAdjust}{\baselineskip}

    \colorlet{shadecolor}{LavenderBlush2}
    \colorlet{framecolor}{Red1}

    \newenvironment{frshaded}{%
    \def\FrameCommand{\fboxrule=\FrameRule\fboxsep=\FrameSep \fcolorbox{framecolor}{shadecolor}}%
    \MakeFramed {\FrameRestore}}%
    {\endMakeFramed}

    \newenvironment{frshaded*}{%
    \def\FrameCommand{\fboxrule=\FrameRule\fboxsep=\FrameSep \fcolorbox{framecolor}{shadecolor}}%
    \MakeFramed {\advance\hsize-\width \FrameRestore}}%
    {\endMakeFramed}

    \begin{document}

    \section{Title}
    \blindtext

    \section{Title}
    \blindtext
    \vspace*{\baselineskip}
    \begin{frshaded*}\vspace*{-\baselineskip}
    \section{Title}
    \blindtext

    \blindtext
    \section{Title}
    \blindtext
    \end{frshaded*}

    \vspace*{\baselineskip}
    \begin{frshaded}\vspace*{-\baselineskip}
    \section{title}
    \blindtext

    \section{title}
    \blindtext
    \end{frshaded}

    \end{document} 

enter image description here

enter image description here

share|improve this answer
    
Thanks very much for the example. I do not know whether the content in the box actually starts with a section or not. Therefore, I would like to avoid the use of an explicit vspace with a negative amount. Can I avoid this approach without using a minipage? –  Jan Christiansen Aug 18 at 14:56

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.