3

Starting a new project (script for a lecture) I would like to put wide graphs partly into the margin. The following MWE shows the current situation (very much simplified).

\documentclass[parskip=true]{scrbook}
\usepackage{calc}
\usepackage[latin1]{inputenc}
\usepackage[T1]{fontenc}
\setlength{\textwidth}{10cm}
\setlength{\marginparsep}{1cm}
\setlength{\marginparwidth}{5cm}
\begin{document}
\section*{Version 1}

foo

\rule{\textwidth}{1pt}

\begin{figure}[h]
 \makebox[\textwidth]{\rule{\textwidth+\marginparsep+0.6\marginparwidth}{1cm}}
 \caption{bla}
\end{figure}

bar

\newpage

foo

\rule{\textwidth}{1pt}

\begin{figure}[h]
 \makebox[\textwidth]{\rule{\textwidth+\marginparsep+0.6\marginparwidth}{1cm}}
 \caption{bla}
\end{figure}

bar

\newpage

\section*{Version 2}

foo

\rule{\textwidth}{1pt}

\begin{figure}[h]
 \rule{\textwidth+\marginparsep+0.6\marginparwidth}{1cm}
 \caption{bla}
\end{figure}

bar

\newpage

foo

\rule{\textwidth}{1pt}

\begin{figure}[h]
 \rule{\textwidth+\marginparsep+0.6\marginparwidth}{1cm}
 \caption{bla}
\end{figure}

bar

\end{document}

I need the figure to be left-justified on the odd pages and to be right-justified on the even pages (not so surprising). Both approaches (versions 1 and 2) do not yield this result.

The width of the graph will vary and any manual adjustment is not acceptable. Of course I can put each graph into a box, measure its width and set it according to this value. But I wonder (and hope) if there is simpler solution. Maybe this is already implemented in a package?

Jürgen
  • 2,160

1 Answers1

4

You can use KOMA-Script's addmargin* environment in combination with \ifthispageodd:

\documentclass[parskip=true]{scrbook}
\usepackage{calc}
\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}
\usepackage{mwe}

\usepackage{showframe}% visualise the page areas

\begin{document}
\section*{Version 1}

\begin{figure}[h]
  \begin{addmargin*}[0pt]{-\marginparsep-\marginparwidth}
    \includegraphics[width=\linewidth]{example-image-a}
    \caption{bla}
  \end{addmargin*}
\end{figure}

\newpage

\begin{figure}[h]
  \begin{addmargin*}[0pt]{-\marginparsep-\marginparwidth}
    \includegraphics[width=\linewidth]{example-image-a}
    \caption{bla}
  \end{addmargin*}
\end{figure}

\section*{Version 2}

\begin{figure}[h]
  \begin{addmargin*}[0pt]{-\marginparsep-\marginparwidth}
    \ifthispageodd{\raggedright}{\raggedleft}
    \includegraphics[width=.9\linewidth]{example-image-a}
    \caption{bla}
  \end{addmargin*}
\end{figure}

\newpage

\begin{figure}[h]
  \begin{addmargin*}[0pt]{-\marginparsep-\marginparwidth}
    \ifthispageodd{\raggedright}{\raggedleft}
    \includegraphics[width=.9\linewidth]{example-image-a}
    \caption{bla}
  \end{addmargin*}
\end{figure}

\end{document}

Results in:

Page 1+2Page 3+4

Schweinebacke
  • 26,336
  • Yes, that's it! Many thanks for your solution. (I was just trying to get it running, but you are of course faster!) – Jürgen Feb 23 '17 at 08:51
  • What about for different classes, like -for example- memoir? – Alessandro Cuttin Dec 11 '17 at 15:40
  • @AlessandroCuttin I don't know much about memoir. See the manual if it has a similar environment and a similar commend. Otherwise try scrextend. The package has been made to provide several KOMA-Script features to users of other classes. – Schweinebacke Dec 12 '17 at 07:57
  • Thanks! I posted a question here ;) https://tex.stackexchange.com/q/405635/177 – Alessandro Cuttin Dec 12 '17 at 08:30