# Span figure across margin and textbody in twoside: Assigning savebox locally

For twosidedocuments, I'd like my wide images and tables to span both the margin and the text. In my approach, I have created a command, which takes the input and stores in in a save box, measures its total height, and places in in a minipagein the margin. The minipage spans the width of the margin and the text body.

The reason I had to use a saveboxwas that, since I am placing a minipagein the margin, it takes 0 vertical space in the text body, so I need to measure the height of it and place a vspace in the text to shift the material that follows down accordingly to that height.

Now this works great for the first run, but as soon as I call the command again to place another wide image, I get an error. I understand why I get this error; I am trying to overwrite an existing savebox. I thought when writing the code that it would be saved only locally, not globally. Is there any way I could do that? Assign it locally, that is, or perhaps just delete it afterwards.

In the following MWE, commenting out the line will give the error.

\documentclass[11pt, twoside]{article}
\usepackage{graphicx, showframe}
\newlength\marginandtext
\newcommand{\leftsidefullwidth}[1]{%
\newsavebox\mybox
\newlength\myheight
\newlength\mydepth
\savebox\mybox{\noindent#1}
\settoheight\myheight{\usebox\mybox}
\settodepth\mydepth{\usebox\mybox}
\marginpar{\begin{minipage}{\marginandtext}
\usebox\mybox
\end{minipage}}
\vspace{\myheight}
}
\begin{document}
This page is left empty for this example, since it needs to be shown on left pages.
\clearpage
\leftsidefullwidth{\includegraphics[width=\marginandtext,height=6cm]{example-image-a}}
foo
%\leftsidefullwidth{\includegraphics[width=\marginandtext,height=3cm]{example-image-b}}
foo
\end{document}


• Since you are taking up space in teh text flow it would be more natural to use a normal float than a marginpar and just make the image as wide as you need and shift to the right or left so that it sticks into the margin space. – David Carlisle Jan 4 '16 at 10:55
• as for the actual question always have the \newxxx command at the top level, you do not need to allocate a new box each time, just allocate a box for your package and re-use that box each time. Also you need % the ends of all those lines or your command will generate spurious white space in the text. – David Carlisle Jan 4 '16 at 10:56
• really? I thought I checked that a while ago. Guess I was completely wrong here. Okey, I guess this was a really stupid question on my part. – Runar Jan 4 '16 at 11:04

you do not need a box or measurement at all here as

\savebox\mybox{\noindent#1}


\savebox is a horizontal construct (like \mbox) so \noindent is a no-op and the box is the size of its contents. You know that size in advance as you have

[width=\marginandtext,height=6cm]


You should use a normal float not a marginpar, as the float takes space in the main text flow, then use \makebox[\textwidth][l]{begin{minipage}{\marginandtext}} on one side and [r] on the other.

• Just to be clear, I wouldn't always know the size of the box (for instance a table), that was just for example images. – Runar Jan 4 '16 at 11:09
• @runartrollet you could just use a scratch box (such as 0) for local measurement, but actually you know the width in advance (\marginandtext) and you only need the height to correct for the misuse of \marginpar, if you used a normal float you wouldn't need that vspace correction. – David Carlisle Jan 4 '16 at 11:12
• Any way to dynamically set [r]and [l]in \makebox[\textwidth][l]{begin{minipage}{\marginandtext}}depending on odd/even pages? – Runar Jan 4 '16 at 11:45
• @runartrollet I have an answer on site somewhere about page dependent formatting in floats I'll see if I can find.... – David Carlisle Jan 4 '16 at 11:49
• @runartrollet this is probably more general than you need for just odd/even but... tex.stackexchange.com/questions/118001/… – David Carlisle Jan 4 '16 at 11:51