# How to make a new figure environment that does additional stuff?

Currently I have a document in which most caption of figures and tables is placed in the margin, using mcaption. But for some figures and tables I need the full width (textwidth and margin). Both settings are working fine, however I like that \begin{figure} and \begin{table} automatically assumes that it is a margin-captured-floatelement. For pictures over the full width I like to have a new environment \begin{fullwidthfigure} and \begin{fullwidthtable}.

\documentclass{article}
\usepackage{mwe}

\usepackage[
left=2.cm,
right=6.5cm,
marginparwidth=4.5cm,
marginparsep=5mm,
a4paper]{geometry} % Page margins

\usepackage{caption}
\usepackage{mcaption}
\usepackage{etoolbox}
\preto\margincap{\captionsetup{options=margincap}}

\begin{document}
\noindent Some text.
\begin{figure} [h]
\begin{margincap}
\centering
\includegraphics[width=0.4\linewidth]{example-image-a}
\caption{A caption in the margin.}
\end{margincap}
\end{figure}

\noindent Some more text.
\begin{figure}[h]
\checkoddpage
\edef\side{\ifoddpage l\else r\fi}%
\makebox[\textwidth][\side]{%
\begin{minipage}[t]{\textwidth+\marginparsep+\marginparwidth}
\centering
\includegraphics[width=\textwidth]{example-image-b}
\caption{A caption for a fullwidth picture. The caption also goes all the way form the left to the right side.}
\end{minipage}%
}%
\end{figure}

\noindent Some more text.

\end{document}


Edit: I've just seen, that the second figure does not take the whole width. Is this due to a missing package for the + operation? It is working in my other document...

• You need the calc package for the addition of the lengths, otherwise ++ will appear, as you already noticed – user31729 Aug 14 '15 at 2:37
• You could also use the sidenotes package. It provides the described figure and table environments. – Andy Aug 14 '15 at 8:25

There are two problems in your code.

1. As mentioned in a comment of @ChristianHupfer you need to call package calc. Then your code will execute your + as wished ...
2. You need to change the used \textwidth for your second \includegraphics command to \linewidth. The reason for this that you changed at last the linewidth of the used minipage to \textwidth+\marginparsep+\marginparwidth (which is now calculated by package calc).
3. I added one version for the big image without environment figure, the second one with. Please see the comments I added. because youuse class article you do not have to check for odd page or not ...

Complete MWE (see important lines marked with <=======):

\documentclass{article}
\usepackage{mwe}

\usepackage[
left=2.cm,
right=6.5cm,
marginparwidth=4.5cm,
marginparsep=5mm,
a4paper,
showframe,
]{geometry} % Page margins

\usepackage{caption}
\usepackage{mcaption}
\usepackage{calc} % <===================================================
\usepackage{etoolbox}
\preto\margincap{\captionsetup{options=margincap}}

\begin{document}
\noindent Some text.
\begin{figure}%[h]
\begin{margincap}
\centering
\includegraphics[width=0.4\linewidth]{example-image-a}
\caption{A caption in the margin.}
\end{margincap}
\end{figure}

\noindent Some more text.

\noindent % <======================= no indention for following minipage
\begin{minipage}[t]{\textwidth+\marginparsep+\marginparwidth}
\centering
\includegraphics[width=\linewidth]{example-image-b} % <===============
\captionof{figure}{A caption for a fullwidth picture. The caption also goes all the way form the left to the right side.}
\end{minipage}%

\noindent Some more text 2.

\begin{figure}%[h]
% \checkoddpage
% \edef\side{\ifoddpage l\else r\fi}%
% \makebox[\textwidth][\side]{%
\noindent % <======================= no indention for following minipage
\begin{minipage}[t]{\textwidth+\marginparsep+\marginparwidth}
\centering
\includegraphics[width=\linewidth]{example-image-b} % <===============
\caption{A caption for a fullwidth picture. The caption also goes all the way form the left to the right side.}
\end{minipage}%
% }%
\end{figure}

\end{document}


and the result (page 1):