2

I want to recreate the text boxes shown in the figure below. This is from the book Doctor Lawd's labyrinth The box starts in the page while extending till the margin.

enter image description here How can this be achieved? In MWE below I have used the solution suggested here using mdframed. I have added and adjusted the leftmargin and rightmargin with trial and error to achieve a similar/workable result. But I would like to add the lengths so that solution is generic using textwidth and marginpar. Another issue in this approach is that there is blank space above and below the box. Can this space be utilised in wrapping the regular text above/below the box (I have tried setting skipabove=0pt but has not effect on that).

enter image description here

Also finally this solution works for article class but if you set the documentclass to book the boxes are inside the main textwidth and don't extend till the marginpar. And for even pages I would like the boxes on the left-hand side of the page (as seen in the first page layout from the old book), not on the right-hand side (which is correct for odd pages).

MWE

\documentclass{article}
\usepackage{lipsum}
\usepackage{showframe}
\usepackage{wrapfig}
\usepackage[framemethod=tikz]{mdframed}
\usepackage{lipsum}

\newenvironment{WrapText}[1][r] {\wrapfigure{#1}{0.3\textwidth}\mdframed[backgroundcolor=gray!20, hidealllines=true,leftmargin=0, rightmargin=-0.25\textwidth,skipabove=0pt,skipbelow=0pt]} {\endmdframed\endwrapfigure}

\begin{document}

\lipsum[1] \marginpar{This is some margin par}

\begin{WrapText} The quick brown fox jumps right over the lazy dog. the quick brown fox jumps right over the lazy dog. the quick brown fox jumps right over the lazy dog. the quick brown fox jumps right over the lazy dog. the quick brown fox jumps right over the lazy dog. the quick brown fox jumps right over the lazy dog. the quick brown fox jumps right over the lazy dog. the quick brown fox jumps right over the lazy dog. \end{WrapText}

\lipsum[1] \clearpage \newpage

\lipsum[2] \marginpar{This is some margin par}

%\hspace{4cm} % this doesnt work \begin{WrapText} The quick brown fox jumps right over the lazy dog. the quick brown fox jumps right over the lazy dog. the quick brown fox jumps right over the lazy dog. the quick brown fox jumps right over the lazy dog. the quick brown fox jumps right over the lazy dog. the quick brown fox jumps right over the lazy dog. the quick brown fox jumps right over the lazy dog. the quick brown fox jumps right over the lazy dog. \end{WrapText}

\lipsum

\end{document}

enter image description here

Damitr
  • 1,889

1 Answers1

1

I suggest to use tcolorbox instead of mdframed. This package is actively developed and provides many options.

In particular, you can enlarge the box to the margins with the key grow to right by:

\begin{tcolorbox}[grow to right by=\marginparsep+\marginparwidth]
  my box content
\end{tcolorbox}

and also handle odd/even pages with the key toggle enlargement:

\begin{tcolorbox}
  [%
    grow to right by=\marginparsep+\marginparwidth,
    toggle enlargement=evenpage
  ]
  my box content
\end{tcolorbox}

Full example, with option twoside passed to the article class:

\documentclass[twoside]{article}
\usepackage{lipsum}
\usepackage{showframe}
\usepackage{wrapfig}
\usepackage{lipsum}

\usepackage[many]{tcolorbox}

\newenvironment{WrapText} {\wrapfigure{o}{0.3\textwidth} \begin{tcolorbox} [% enhanced, boxrule=0pt, frame hidden, colback=gray!20, sharp corners, grow to right by=\marginparsep+\marginparwidth, toggle enlargement=evenpage, ] } {\end{tcolorbox}\endwrapfigure}

\begin{document}

\lipsum[1] \marginpar{This is some margin par}

\begin{WrapText} The quick brown fox jumps right over the lazy dog. the quick brown fox jumps right over the lazy dog. the quick brown fox jumps right over the lazy dog. the quick brown fox jumps right over the lazy dog. the quick brown fox jumps right over the lazy dog. the quick brown fox jumps right over the lazy dog. the quick brown fox jumps right over the lazy dog. the quick brown fox jumps right over the lazy dog. \end{WrapText}

\lipsum[1] \clearpage \newpage

\lipsum[2] \marginpar{This is some margin par}

%\hspace{4cm} % this doesnt work \begin{WrapText} The quick brown fox jumps right over the lazy dog. the quick brown fox jumps right over the lazy dog. the quick brown fox jumps right over the lazy dog. the quick brown fox jumps right over the lazy dog. the quick brown fox jumps right over the lazy dog. the quick brown fox jumps right over the lazy dog. the quick brown fox jumps right over the lazy dog. the quick brown fox jumps right over the lazy dog. \end{WrapText}

\lipsum

\end{document}

tcolorbox solution, odd page

tcolorbox solution, even page

Edit : shrink spaces before/after the box

Spaces below and after the box are added by wrapfigure. To remove them, add negative \vspace before and after the box with the keys before and after:

\newenvironment{WrapText}
{\wrapfigure{o}{0.3\textwidth}
  \begin{tcolorbox}
    [%
      enhanced, boxrule=0pt, frame hidden,
      colback=gray!20,
      sharp corners,
      grow to right by=\marginparsep+\marginparwidth,
      toggle enlargement=evenpage,
      before={\vspace*{-1em}},
      after={\vspace*{-1em}},
      ]
    }
  {\end{tcolorbox}\endwrapfigure}
jlab
  • 1,834
  • 1
  • 13
  • This looks neat! Can anything be done about the spaces above and below the text box? (Marked in the second image of the question.) – Damitr Mar 30 '24 at 10:47
  • @Damitr Add negative vspace. See the edited answer. – jlab Mar 30 '24 at 10:58