4

As shown in the MWE below, I am using graphicsx in conjunction with adjustbox. I have a document with many figures and would like to automate the "inner" (as opposed to "center") placement of figures that exceed the textwidth. I have spent a lot of time with LaTeX but this is beyond me and my searches for a solution have been fruitless so far. Thanks for any help!

P.S. My MWE probably could be more minimal but I have included all packages I use in this document that have anything to do with floats or graphics.

\documentclass[
   paper=8.5in:11in,
   BCOR=0.5in,
   twoside=true
]{scrbook}
\areaset[0.5in]{5in}{8in}
\addtolength{\voffset}{0.5in}
\setlength{\marginparwidth}{0.5in}
\setlength{\marginparsep}{1in}
\usepackage[showframe,pass]{geometry}

\usepackage{scrhack}
\usepackage{float}
\usepackage{graphicx}
\usepackage[figurename=Example]{caption}
\usepackage{subcaption}
\usepackage{newfloat}

%% Adjust box adds "inner" and "center" options to includegraphics
\usepackage[export]{adjustbox}

\begin{document}

\mainmatter

I would like to match the behavior of this:

\begin{figure}[h!t]
    \includegraphics[scale=1,inner]{example-image-16x10}
    \captionbelow{Captions always centered on textwidth}

    \includegraphics[scale=0.5,center]{example-image-16x10}
    \captionbelow{Captions always centered on textwidth}
\end{figure}

\newpage
Here:

\newcommand*{\myincludegraphics}[2]{%
    \includegraphics[#1,center%% some conditional argument
    ]{#2}%
}

\begin{figure}[h!t]
    \myincludegraphics{scale=1}{example-image-16x10}
    \captionbelow{Captions always centered on textwidth}

    \myincludegraphics{scale=0.5}{example-image-16x10}
    \captionbelow{Captions always centered on textwidth}
\end{figure}

\end{document}
2
  • Could be \myincludegraphics{scale=1, inner}{example-image-16x10} a solution?
    – Ignasi
    Commented Dec 17, 2015 at 17:41
  • No, I want LaTeX to assess the width of the graphic and then position accordingly. The MWE is just an illustration; in reality, I have hundreds of floats and I want to automate the process.
    – user86012
    Commented Dec 17, 2015 at 19:43

3 Answers 3

3

You can directly use \makebox:

\documentclass[
   paper=8.5in:11in,
   BCOR=0.5in,
   twoside=true
]{scrbook}
\areaset[0.5in]{5in}{8in}
\addtolength{\voffset}{0.5in}
\setlength{\marginparwidth}{0.5in}
\setlength{\marginparsep}{1in}
\usepackage[showframe,pass]{geometry}

\usepackage{scrhack}
\usepackage{float}
\usepackage{graphicx}
\usepackage[figurename=Example]{caption}
\usepackage{subcaption}
\usepackage{newfloat}

\usepackage{adjustbox}

\newsavebox{\igbox}
\newcommand{\igdecide}{%
  \ifdim\wd\igbox<\textwidth
    c%
  \else
    \ifoddpage
      l%
    \else
      r%
    \fi
  \fi
}
\newcommand{\fincludegraphics}[2][]{%
  \par
  \sbox\igbox{\includegraphics[#1]{#2}}%
  \checkoddpage
  \makebox[\textwidth][\igdecide]{\usebox{\igbox}}%
}

\begin{document}

\mainmatter

\begin{figure}[!ht]

\fincludegraphics[width=1.1\textwidth]{example-image-16x10}
\captionbelow{Captions always centered on textwidth}

\fincludegraphics[width=0.7\textwidth]{example-image-16x10}
\captionbelow{Captions always centered on textwidth}

\end{figure}

\newpage

\begin{figure}[!ht]

\fincludegraphics[width=1.1\textwidth]{example-image-16x10}
\captionbelow{Captions always centered on textwidth}

\fincludegraphics[width=0.7\textwidth]{example-image-16x10}
\captionbelow{Captions always centered on textwidth}
\end{figure}

\end{document}

enter image description here

3
  • When I recompile your code now the images are smaller!? Maybe I created them with a different density on update of the mwe package... Commented Jan 5, 2019 at 19:01
  • @MartinScharrer I changed the code to use width rather than scale.
    – egreg
    Commented Jan 5, 2019 at 19:09
  • That what I did as well. I have to check on the images in mwe still. Commented Jan 5, 2019 at 19:16
1
\documentclass[
   paper=8.5in:11in,
   BCOR=0.5in,
   twoside=true
]{scrbook}
\areaset[0.5in]{5in}{8in}
\addtolength{\voffset}{0.5in}
\setlength{\marginparwidth}{0.5in}
\setlength{\marginparsep}{1in}
\usepackage[showframe,pass]{geometry}

\usepackage{scrhack}
\usepackage{float}
\usepackage{graphicx}
\usepackage[figurename=Example]{caption}
\usepackage{subcaption}
\usepackage{newfloat}

%% Adjust box adds "inner" and "center" options to includegraphics
\usepackage[export]{adjustbox}

% customizing LaTeX innards. Still possible, for the time being.
\makeatletter
\def\bm@IC{\hss\edef\width{\dimexpr\the\width\relax}%
           \ifdim\width>\linewidth
             \ifoddpageoroneside\kern\dimexpr\width-\linewidth\fi
          \fi
           \unhbox\@tempboxa
           \ifdim\width>\linewidth 
             \unless\ifoddpageoroneside\kern\dimexpr\width-\linewidth\fi
           \fi
          \hss}

\newcommand*{\myincludegraphics}[2][]{%
    {\parindent\z@ \parfillskip\z@skip\checkoddpage
     \makebox[\linewidth][IC]{\includegraphics[#1]{#2}}\par}%
}

\makeatother


\begin{document}

\mainmatter

I would like this behavior :

\begin{figure}[h!t]
    \myincludegraphics[scale=1]{example-image-16x10}
    \captionbelow{Captions always centered on textwidth}

    \myincludegraphics[scale=0.5]{example-image-16x10}
    \captionbelow{Captions always centered on textwidth}
\end{figure}

\newpage

\begin{figure}[h!t]
    \myincludegraphics[scale=1]{example-image-16x10}
    \captionbelow{Captions always centered on textwidth}

    \myincludegraphics[scale=0.5]{example-image-16x10}
    \captionbelow{Captions always centered on textwidth}
\end{figure}

\newpage

\begin{figure}[h!t]
    \includegraphics[scale=1, inner]{example-image-16x10}
    \captionbelow{Captions always centered on textwidth}

    \includegraphics[scale=0.5, center]{example-image-16x10}
    \captionbelow{Captions always centered on textwidth}
\end{figure}

\newpage

\begin{figure}[h!t]
    \includegraphics[scale=1, inner]{example-image-16x10}
    \captionbelow{Captions always centered on textwidth}

    \includegraphics[scale=0.5, center]{example-image-16x10}
    \captionbelow{Captions always centered on textwidth}
\end{figure}

\end{document}

enter image description here

enter image description here

1
  • the \edef\width{\dimexpr\the\width\relax} is because the \unhbox\@temboxa empties the box register, and \width which is macro \wd\@tempboxa does not work after it. I could have done \unhcopy, or a solution using a temp dimen, there are many ways.
    – user4686
    Commented Dec 18, 2015 at 10:24
0

Here a solution based on adjustbox using precode key. Unfortunately it is not possible to do this with a single call of \adjustimage as the \width key then does hold not yet the new scaled width but the original one. Therefore nesting the \includegraphics or \adjustbox inside \adjustbox is required.

\documentclass[
   paper=8.5in:11in,
   BCOR=0.5in,
   twoside=true
]{scrbook}
\areaset[0.5in]{5in}{8in}
\addtolength{\voffset}{0.5in}
\setlength{\marginparwidth}{0.5in}
\setlength{\marginparsep}{1in}
\usepackage[showframe,pass]{geometry}

\usepackage{scrhack}
\usepackage{float}
\usepackage{graphicx}
\usepackage[figurename=Example]{caption}
\usepackage{subcaption}
\usepackage{newfloat}

%% Adjust box adds "inner" and "center" options to includegraphics
\usepackage[export]{adjustbox}


\begin{document}

\mainmatter

I would like to match the behavior of this:

\begin{figure}[h!t]
    \includegraphics[scale=1.2,inner]{example-image-16x10}
    \captionbelow{Captions always centered on textwidth}

    \includegraphics[scale=0.5,center]{example-image-16x10}
    \captionbelow{Captions always centered on textwidth}
\end{figure}

\newpage
Here:

\newcommand*{\myincludegraphics}[2]{%
    \adjustbox{precode=%
        \ifdim\width>\textwidth
            \expandafter\innersidebox
        \else
            \expandafter\centerbox
        \fi
    }{\includegraphics[#1]{#2}}%
}

\begin{figure}[h!t]
    \myincludegraphics{scale=1.2}{example-image-16x10}
    \captionbelow{Captions always centered on textwidth}

    \myincludegraphics{scale=0.5}{example-image-16x10}
    \captionbelow{Captions always centered on textwidth}
\end{figure}

\newpage
And here:

\begin{figure}[h!t]
    \myincludegraphics{scale=1.2}{example-image-16x10}
    \captionbelow{Captions always centered on textwidth}

    \myincludegraphics{scale=0.5}{example-image-16x10}
    \captionbelow{Captions always centered on textwidth}
\end{figure}

\end{document}

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .