1

Some context: I use Inkscape to produce pictures in pdf format, that I import in latex using the usual function \includegraphics{pdffile}. Apart from keeping the picture in a vectorial format, it also sets the right size for the picture without any intervention from me.

The problem is, sometimes I am obliged to produce a pdf whose dimension go over the width of the page. That can easily be corrected manually, using

\includegraphics[width=\pagewidth]{pdffile}

, but what I would like, it some trick to tell latex to set automatically all figures to the width of the page if it goes over, or to keep the original sizes if not.

Here is an MWE:

\documentclass[10pt,a4paper]{article}
\usepackage{fontspec,graphicx}
\usepackage[left=2cm,right=5cm,top=5cm,bottom=2cm]{geometry}
\begin{document}

Some text.

\begin{figure}[h]
\begin{center}
   \includegraphics{example-image-a.pdf}
\end{center}
\caption{Some too long PDF.}
\end{figure}

\end{document}
cfr
  • 198,882
HcN
  • 1,274
  • 9
  • 25
  • I've edited your question to use a PDF which is part of all standard distributions, being included precisely so that it can be used in MWEs which require such things. If you prefer, you could use example-image-b or example-image-c or one of the other exciting options included in the mwe package ;). Also, you need graphicx. – cfr Sep 29 '15 at 00:27

1 Answers1

4

This defines a new command \includeimage[]{}. This works just like \includegraphics[]{} except it limits the width to \textwidth if required and if no width is set manually in the options to override this. That is, you can still override the maximum if you choose.

This shows the difference between \includegraphics{example-image-a.pdf} and \includeimage{example-image-a.pdf}:

old versus new

Code:

\documentclass[a5paper]{article}
\usepackage[showframe]{geometry}
\usepackage{graphicx,xparse}
\makeatletter
  \newlength\img@width
  \NewDocumentCommand\includeimage { O {} m }{%
    \settowidth\img@width{\includegraphics[#1]{#2}}%
    \ifdim\img@width > \textwidth \includegraphics[width=\textwidth, #1]{#2}%
    \else \includegraphics[#1]{#2}5
    \fi}
\makeatother
\begin{document}

Some text.

\begin{figure}
  \centering
  \includegraphics{example-image-a.pdf}
  \caption{Some too long PDF.}
\end{figure}
\begin{figure}
  \centering
  \includeimage{example-image-a.pdf}
  \caption{Some too long PDF.}
\end{figure}

\end{document}

Note:

  • It is better to say \includegraphics{example-image-a} without the extension in most cases. The same for \includeimage{example-image-a}.

  • Don't use the center environment as well as figure as it adds extra vertical space.

  • Don't use \begin{figure}[h]. If you really want something to be here, don't use figure at all. (If you want a caption, use caption or capt-of which support captions outside floats.)

cfr
  • 198,882
  • Works like a charm. Thanks a lot indeed. Also I finally understood where is this strange vertical space on my figures coming from. But I am confused why is it better not to include the extension, or what is the problem with \begin{figure}[h] ? – HcN Sep 29 '15 at 10:36
  • @HcN Floats are complicated: have a read of this answer. – cfr Sep 29 '15 at 21:18