[Tex/LaTex] Layout with captions in margin and margin notes

captionsmarginparmargins

Questions summed up below!

I want to convert a number of old (read: ugly self-coded html) tutorials into pdfs. They are still read by people, so I think it's worth the effort. The other reasons:

  • readers repeatedly asked for a printable version, for side-reading;

  • they want to print them out to make notes and drawings (I could also interpret this as a demand for rephrasing, but I don't.);

  • I want to use what I have learned about typography.

My approach starts with the demand for space for personal notes. They will probably go into the margin, because the margin usually provides some space. The tutorials are aimed at beginners in the subject, and a lot of the content is extremely simplified. This calls for inline notes, footnotes or marginal notes. I want a wide margin for the readers' personal notes, and I also want to use the margin for notes in the tutorial.

Now that the margin is wide, I can also place figure and table captions (I need both!) in the margin, and this is where I'm stuck. I've tried Will Robertson's suggestion from this Q&A, but the marginal caption is placed on top of the text. The code below is his, but turned in to an MWE:

\documentclass{article}

\usepackage{lipsum}

\makeatletter
\newcommand\margincaption[1]{%
  \par
  \setbox\@tempboxa=\hbox{\parbox{\marginparwidth}{\caption{#1}}}
  \@tempdima=\dimexpr\ht\@tempboxa+\dp\@tempboxa\relax
  \par\null\hfill\usebox\@tempboxa\par
  \vspace*{\dimexpr-\@tempdima-\baselineskip\relax}
}
\makeatother

\begin{document}

\lipsum[1]
\begin{figure}[tp]
\rule{\textwidth}{1em}
\margincaption{A black long line with lots of explanation so it fills up some space.}
\end{figure}
\lipsum[2]
\end{document}

I have tried ConTeXt for this, but the caption setting rightmargin produces no caption at all. I have asked the mailing list, but I've got the impression that it might be possible with pdfLaTeX (or XeTeX, or luaLaTeX) as well.

Yet another issue pops up when I need to include long lines of example code. The margin is wide, so the main text area is narrower than usual, which could lead to collisions. Anyway, is there a way to include over-long lines without generating an overfull hbox warning? This is a minor issue though – long lines can be broken.

  • How can I place figure and table captions in the margin?

  • How can I include single lines, extending into the margin, without producing collosions?

  • If you have the impression that my layout approach is not suitable, do you have a brilliant idea I could try instead?

Best Answer

As others have said, you can use the tufte-book document class. It offers captions in the sidenotes area and a fullwidth environment that stretches across the main text block and the sidenotes area.

\documentclass{tufte-book}
\usepackage{lipsum}

\begin{document}

\lipsum[1]
\begin{figure}[!ht]
  \centering
  \rule{4cm}{2cm}
  \caption{A black rectangle with lots of text so it fills up some space.}
\end{figure}
\lipsum[1]
\begin{table}[!ht]
  \centering
  \rule{4cm}{2cm}
  \caption{A black rectangle with lots of text so it fills up some space.}
\end{table}
\begin{fullwidth}
\lipsum[1]
\end{fullwidth}

\end{document}

If you don't want to use the tufte-book class, then you can use one of the standard ones (book, report) together with the geometry package to change the page layout, the floatrow package to have captions in the margins, and the changepage package to change the margins for a part of a document:

\documentclass{book}
\usepackage[margin=4cm,marginparwidth=3cm]{geometry}
\usepackage{floatrow}
\usepackage{changepage}
\usepackage{lipsum}

\floatsetup{margins=hangleft,capposition=beside,
capbesideposition={top,left},floatwidth=\textwidth}

\begin{document}

\lipsum[1]
\begin{figure}[!ht]
  \centering
  \rule{4cm}{2cm}
  \caption{A black rectangle with lots of text so it fills up some space.}
\end{figure}
\lipsum[1]
\begin{table}[!ht]
  \centering
  \rule{4cm}{2cm}
  \caption{A black rectangle with lots of text so it fills up some space.}
\end{table}
\begin{adjustwidth}{-3cm}{}
\lipsum[1]
\end{adjustwidth}

\end{document}

You could also consider using one of the classes from the KOMA-Script bundle (scrbook, scrreprt) or the memoir document class and adapting them to your needs.