2

I have produced my pdf output from tex file and noticed that my pictures are not "correctly" included - their left margin corresponds to the left margin of the regular text, but the right margin in beyond my control and stands out:

enter image description here

To address this problem, I would like to make all icnluded pictures be centered and NOT be subjected to any text margin settings (they may exceed text margins, but symmetrically). Moreover, in my case, it must be done via some global options at the beginning of the document (NOT: in the document body, e.g. within includegraphics section).

Thank you in advance for any suggestions!

4
  • Your picture is too wide for the page: centring a picture that 'fits' is easy enough
    – Joseph Wright
    Commented Feb 26, 2014 at 17:47
  • you can use \centering in the figure and use \width=1\textwidth in the square brackets after \includegraphics. If you need something global, you can define a macro using \def to replace the usual \includegraphics.
    – yoki
    Commented Feb 26, 2014 at 17:53
  • I am also wondering what happened to second image on the right margin (with 'RStudio' title)?
    – user31729
    Commented Feb 26, 2014 at 18:09
  • (Well, on the picture I included in my post there is one image, which consists of two "Paint-sticked" screen shoots ;) ) Commented Feb 27, 2014 at 8:17

1 Answer 1

3

I save (\let) a copy of \includegraphics to \svincludegraphics and then redefine it with

\renewcommand\includegraphics[2][]{\makebox[0pt]{\svincludegraphics[#1]{#2}}}

so that it appears to LaTeX as being centered about a zero-width box (I could make that a \textwidth box if I wanted). As long as the \includegraphics usage is centered on a line by itself, it will freely extend past the margins, as needed.

\documentclass{article}
\usepackage[demo]{graphicx}
\usepackage{lipsum}
\let\svincludegraphics\includegraphics
\renewcommand\includegraphics[2][]{\makebox[0pt]{\svincludegraphics[#1]{#2}}}
\begin{document}
\lipsum[3]
\begin{figure}[ht]
\centering
\includegraphics[width=1.1\textwidth,height=1cm]{file}
\caption{My caption}
\end{figure}
\lipsum[4]
\begin{figure}[ht]
\centering
\includegraphics[width=0.7\textwidth,height=1cm]{file}
\caption{My caption}
\end{figure}
\lipsum[4]
\end{document}

enter image description here

3
  • 1
    +1 Your answer is shorter than mine and does basically the same. so I'll delete mine. Commented Feb 26, 2014 at 18:44
  • Perfect answer! Thank you a lot! I am a basic TeX user and wouldn't work it out for hours. It saved me a plenty of time! :) Commented Feb 27, 2014 at 8:26
  • 1
    @Marciszka You are most welcome. I edited my solution once I realized that I did not need the stackengine package for this solution (part of an earlier attempt). So you can remove that inclusion from your solution. [You'll find I use stackengine a lot, since I wrote it] Commented Feb 27, 2014 at 10:57

You must log in to answer this question.

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