2

I would like to modify the way standard floats, like figure or table, appear on page by shifting them horizontally relative to the paragraph text, see the image below.

enter image description here

To this end I have tried to use the adjustwidth environment provided by the package changepage, and redefine the original environments (e.g. figure) in the following way.

\documentclass[]{article}

\usepackage{mwe}
\usepackage{changepage}

\makeatletter
\newenvironment{shiftedfloat}[2][]
               {\@float{#2}[#1]\begin{adjustwidth}{2cm}{-2cm}}
               {\end{adjustwidth}\end@float}
\renewenvironment{figure}[1][\fps@figure]
               {\shiftedfloat[#1]{figure}}
               {\endshiftedfloat}
\makeatother

\begin{document}

\blindtext

\begin{figure}[h]
  \includegraphics[width=\textwidth]{example-image}
  \caption{Figure caption}
\end{figure}

\end{document}

This only works until I try to load the float package and change the placement specification from [h] to [H], when I get an error:

! Extra }, or forgotten \endgroup.

Interestingly, everything works just fine when I move adjustwidth out of the shiftedfloat definition and put it directly into the given figure:

\documentclass[]{article}

\usepackage{mwe}
\usepackage{float}
\usepackage{changepage}

\makeatletter
\newenvironment{shiftedfloat}[2][]
               {\@float{#2}[#1]}
               {\end@float}
\renewenvironment{figure}[1][\fps@figure]
               {\shiftedfloat[#1]{figure}}
               {\endshiftedfloat}
\makeatother

\begin{document}

\blindtext

\begin{figure}[H]
  \begin{adjustwidth}{2cm}{-2cm}
  \includegraphics[width=\textwidth]{example-image}
  \caption{Figure caption}
  \end{adjustwidth}
\end{figure}

\end{document}

I appears to me that for some reason the \end{adjustwidth} gets lost in the former definition of shiftedfloat, but I couldn't quite figure out why.

Any suggestions how this can be fixed? Or maybe there is a better way of achieving the the desired effect? It's important that the original float environments get altered rather than any new environments are introduced, and that the solution is compatible with the float package, in particular with the [H] argument.

1

Thanks to Axel Sommerfeldt's solution to a similar issue described in Compatibility issue with \renewenvironment{figure} and [H] placement I was able to solve the problem by patching \float@endH from the float package as well.

\documentclass[]{article}

\usepackage{mwe}
\usepackage{changepage}
\usepackage{float}

\makeatletter
\newenvironment{shiftedfloat}[2][]
               {\@float{#2}[#1]\begin{adjustwidth}{2cm}{-2cm}}
               {\end{adjustwidth}\end@float}
\renewenvironment{figure}[1][\fps@figure]
               {\shiftedfloat[#1]{figure}}
               {\endshiftedfloat}               
% Adaption to the [H] placement provided by the float package
\AtBeginDocument{%
  \let\float@endHorg\float@endH
  \def\float@endH{\end{adjustwidth}\float@endHorg}
}
\makeatother

\begin{document}

\blindtext

\begin{figure}[H]
  \includegraphics[width=\textwidth]{example-image}
  \caption{Figure caption}
\end{figure}

\end{document}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.