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
\addtolength{\marginandtext}{\textwidth}
\addtolength{\marginandtext}{\marginparwidth}
\addtolength{\marginandtext}{\marginparsep}
\newcommand{\leftsidefullwidth}[1]{%
\newsavebox\mybox
\newlength\myheight
\newlength\mydepth
\savebox\mybox{\noindent#1}
\settoheight\myheight{\usebox\mybox}
\settodepth\mydepth{\usebox\mybox}
\addtolength{\myheight}{\mydepth}
\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}

\newxxxcommand 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 04 '16 at 10:56