3

The trick indicated here for including an image bigger than \textwidth (with \makebox) works with document class article or with oneside book but not with twoside book, even if I set nomarginpar and \marginparsep = 0pt, what is missing?

\documentclass{book}
\usepackage[nomarginpar]{geometry}
\setlength{\marginparsep}{0pt}
\usepackage{graphicx}
\usepackage{showframe}
\begin{document}
\begin{figure}%
  \makebox[\textwidth][c]{\includegraphics[width=\paperwidth]{example-image}}%
  \caption{My caption}
\end{figure}
\end{document}

enter image description here

P.S. = I know I can use adjustbox, I would like only to know why the trick doesn't work with twoside book class.

CarLaTeX
  • 62,716
  • the code works the same way in book and article, but it overlaps the same amount on both sides so if you have differrent left and right margins it may not do what you want – David Carlisle Nov 29 '21 at 10:05
  • 1
    to get a paperwidth image use a left aligned makebox and start with \hspace{-\@totalleftmargin} to get to the paper edge. – David Carlisle Nov 29 '21 at 10:08
  • @campa Correct, indeed it works with oneside book. – CarLaTeX Nov 29 '21 at 12:13
  • @DavidCarlisle I tried but it doesn't work, where have I to put it? – CarLaTeX Nov 29 '21 at 13:09
  • It's not trivial to answer because many classes/packages redefine the way margins are dealt with. Are you going to use only standard classes with the geometry package? – campa Nov 29 '21 at 13:24
  • Do you try changepage package? With its macro adjustwidth tis should work. \begin{figure}\begin{adjustwidth}{-2em}{-2em} \includegraphics[width=\paperwidth]{example-image}}% \caption{My caption}\end{adjustwidt} \end{figure – Zarko Nov 29 '21 at 13:25
  • @campa I'm interested only in book with geometry. – CarLaTeX Nov 29 '21 at 14:03
  • well naturally it will work if you do what I mean not what I wrote – David Carlisle Nov 29 '21 at 14:03
  • @Zarko See the P.S. at the end of the question. – CarLaTeX Nov 29 '21 at 14:07
  • @DavidCarlisle I'm not so expert to catch what you meant :D – CarLaTeX Nov 29 '21 at 14:08
  • I meant \hspace*{\dimexpr-1in-\csname @totalleftmargin\\endcsname}% \makebox[0pt][l]{\includegraphics[width=\paperwidth]{example-image with an "implied" 1in offset, but campa's answer is better with the odd/even page test – David Carlisle Nov 29 '21 at 14:14
  • @DavidCarlisle Thanks, I've accepted campa's answer. – CarLaTeX Nov 29 '21 at 14:26

2 Answers2

4

The margins of recto/verso pages are usually not symmetric, hence you need to take the correct margin into account, either \Gm@lmargin or \Gm@rmargin (assuming the package geometry is loaded). Checking if we are on an even or odd page isn't trivial, but using e.g. the changepage package we can do

\documentclass{book}

\usepackage{geometry} \usepackage{graphicx} \usepackage{showframe}

\usepackage[strict]{changepage} \newcommand*{\foo}[1]{% choose a better name :-) \checkoddpage \makebox[\textwidth][l]{\kern-\csname Gm@\ifoddpage l\else r\fi margin\endcsname\includegraphics[width=\paperwidth]{#1}}% }

\begin{document}

\begin{figure} \foo{example-image-a} \caption{On a recto page} \end{figure}

\begin{figure} \foo{example-image-b} \caption{On a verso page} \end{figure}

\end{document}

enter image description here

Note that this basically uses labels, so in general a couple of compilations are needed to work. Alternatively you can use the package scrextend (from the KoMa bundle)

\usepackage{scrextend}
\makeatletter
\newcommand*{\foo}[1]{%
   \makebox[\textwidth][l]{\ifthispageodd{\kern-\Gm@lmargin}{\kern-\Gm@rmargin}\includegraphics[width=\paperwidth]{#1}}%
}
\makeatother
campa
  • 31,130
4

As I mentioned in my comment: with \adjustwidth macro defined in the changeapage (it is also part of memoir document class) is simple:

\documentclass[twodside]{book}
\usepackage{geometry}
\usepackage{graphicx}
\usepackage[strict]{changepage}
%---------------- Show page layout. Don't use in a real document!
\usepackage{showframe}
\renewcommand\ShowFrameLinethickness{0.15pt}
\renewcommand*\ShowFrameColor{\color{red}}
%---------------------------------------------------------------%

\begin{document} \begin{figure} \begin{adjustwidth}{-\marginparwidth}{-\marginparwidth} \begin{minipage}{\linewidth} \includegraphics[width=\linewidth]{example-image}% \caption{My caption} \end{minipage} \end{adjustwidth} \end{figure}

\begin{figure}%

\begin{adjustwidth}{-\marginparwidth}{-\marginparwidth} \begin{minipage}{\linewidth} \includegraphics[width=\linewidth]{example-image}% \caption{My caption} \end{minipage} \end{adjustwidth} \end{figure} \end{document}

Note: second argument of adjustwidth* is width of outer margin (usually equal to marginparsep + \marginparidth)

enter image description here

campa
  • 31,130
Zarko
  • 296,517