3

I have a page that has a shaded box at the top. How do I get the shaded box to align with the top of the page frame? There's a bit of a gap. My code:

\documentclass[10pt]{book}
\usepackage[showframe]{geometry}
\usepackage[x11names]{xcolor}
\usepackage{framed}
\colorlet{shadecolor}{LavenderBlush2}
\usepackage{lipsum}
\begin{document}
\setlength{\OuterFrameSep}{0pt}
\begin{shaded*}
\lipsum[1]
\end{shaded*}
\vfill
\begin{shaded*}
\lipsum[1]
\end{shaded*}
\end{document}

results in enter image description here

In another question (Align shaded box to bottom of page), it has been suggested to set OuterFrameSep to 0 in order to remove the vertical space before and after the framed environment. That does seem to work for after the framed environment, but not before.

  • 1
    You can add \leavevmode\vspace{-3.75ex} just before \begin{shaded*} (found by trial and error). – Bernard Apr 16 '22 at 09:14
  • 1
    It's caused by \topskip (initially 10pt). Setting it to 0pt works, but a zero \topskip is not ideal for pages that starts with textual content. – muzimuzhi Z Apr 16 '22 at 09:15

2 Answers2

3

Try the following, it may work for you too:

\documentclass[10pt]{book}
\usepackage{geometry}
\usepackage[x11names]{xcolor}
\usepackage{framed}
\colorlet{shadecolor}{LavenderBlush2}
\setlength{\OuterFrameSep}{0pt}

\usepackage{etoolbox} % <--- \BeforeBeginEnvironment{shaded}{\topskip=0pt} % <--- \AfterEndEnvironment{shaded}{\topskip=10pt} % <--- \usepackage{lipsum} \usepackage{graphicx} %---------------- Show page layout. Don't use in a real document! \usepackage{showframe} \renewcommand\ShowFrameLinethickness{0.15pt} \renewcommand*\ShowFrameColor{\color{red}} %---------------------------------------------------------------%

\begin{document} \begin{shaded} \lipsum[1] \end{shaded}

\vfill \begin{shaded} \lipsum[1] \end{shaded} \clearpage \begin{figure}[ht] \includegraphics[width=\linewidth]{example-image-duck} \end{figure} \section{title}\lipsum \end{document}

enter image description here

Zarko
  • 296,517
1

An alternative is to use the package tcolorbox. Here is a version that gives you a lot of control about essentially everything:

\documentclass[10pt]{book}
\usepackage[showframe]{geometry}
\usepackage[x11names]{xcolor}
\colorlet{shadecolor}{LavenderBlush2}
\usepackage{lipsum}
\usepackage{tcolorbox}
\tcbuselibrary{breakable}
\begin{document}
    \begin{tcolorbox}[boxrule=0mm, boxsep=0mm, sharp corners, colback=shadecolor]
        \lipsum[1]
    \end{tcolorbox}
    \vfill
    \begin{tcolorbox}[boxrule=0mm, boxsep=0mm, 
                      after skip=0pt,  % this removes some extra space at the bottom
                      bottom=2ex, top=2ex, right=2ex, left=2ex,  % 'padding' between text and border
                      sharp corners, colback=shadecolor]
        \lipsum[1]
    \end{tcolorbox}
\end{document}
BanDoP
  • 578