Generalavoiding viewport when using includegraphics with pdf files

LaTeX specific issues not fitting into one of the other forums of this category.
Post Reply
steve6390
Posts: 16
Joined: Sat Nov 29, 2008 6:36 am

avoiding viewport when using includegraphics with pdf files

Post by steve6390 »

Hi folks,
I use the same pdf graphic many times and would like to avoid specifying a viewport in every instance. If the graphic changes, I'd have to redo all these. I created a .bb file, but this is mostly useless with .pdf files. What a drag.

So, I'm interested in a solution that avoids retyping the viewport when I change the source graphic. Preferrably a solution that uses some external file (like .bb) so I don't have to change my .tex file at all.

Here's an example that shows the problem:

Code: Select all

% Comments start with the % character
\documentclass{article}
\usepackage[hiresbb]{graphicx}
\begin{document}

% this one works, no bb file
\begin{figure}[ht]
\setlength\fboxsep{0pt}
\fbox{ \includegraphics[width=1in, viewport=25.877109 204.315111 79.269959 252.823984]{spiral_1_nobb.pdf}}
\end{figure}

% this one works, bb file and viewport
\begin{figure}[ht]
\setlength\fboxsep{0pt}
\fbox{\includegraphics[width=1in,viewport=25.877109 204.315111 79.269959 252.823984]{spiral_1.pdf}}
\end{figure}

% misplaced graphic, bb file only
\begin{figure}[ht]
\setlength\fboxsep{0pt}
\fbox{\includegraphics[width=1in]{spiral_1.pdf}}
\end{figure}

\end{document}
The contents of spiral_1.bb are:

Code: Select all

%%BoundingBox: 25 204 80 253
%%HiResBoundingBox: 25.877109 204.315111 79.269959 252.823984
Attachments
spiral_1.pdf
The pdf graphic.
(1.39 KiB) Downloaded 276 times

Recommended reading 2024:

LaTeXguide.org • LaTeX-Cookbook.net • TikZ.org

NEW: TikZ book now 40% off at Amazon.com for a short time.

And: Currently, Packt sells ebooks for $4.99 each if you buy 5 of their over 1000 ebooks. If you choose only a single one, $9.99. How about combining 3 LaTeX books with Python, gnuplot, mathplotlib, Matlab, ChatGPT or other AI books? Epub and PDF. Bundle (3 books, add more for higher discount): https://packt.link/MDH5p

Juanjo
Posts: 657
Joined: Sat Jan 27, 2007 12:46 am

avoiding viewport when using includegraphics with pdf files

Post by Juanjo »

This thread considers a similar problem. The solution given there may inspire you something that suits your needs.
The CTAN lion is an artwork by Duane Bibby. Courtesy of www.ctan.org.
steve6390
Posts: 16
Joined: Sat Nov 29, 2008 6:36 am

avoiding viewport when using includegraphics with pdf files

Post by steve6390 »

Thanks Juanjo. I also had that problem while experimenting and it's nice to see a solution.
In my case, I put the whole includegraphics line in a macro:

Code: Select all

\newcommand{\spiralone}{\includegraphics[width=\linewidth, viewport=25.877109 204.315111 79.269959 252.823984]{spiral_1}}
...
\begin{figure}
\spiralone
\end{figure}
I'd rather not have to change my source at all. The bbox built-in to .eps files would give this advantage. I've noticed many complaints about the lack of equivalent functionality for pdf.
User avatar
T3.
Posts: 208
Joined: Fri Mar 14, 2008 12:58 pm

avoiding viewport when using includegraphics with pdf files

Post by T3. »

steve6390 wrote:I'd rather not have to change my source at all
In that case why not just crop your pdf to desired size with some pdf-tool? You can even do it with pdflatex alone using preview package, e.g.:

Code: Select all

\documentclass{minimal}
\usepackage[hiresbb]{graphicx}
\usepackage[active,tightpage,graphics]{preview}
\begin{document}
\includegraphics[viewport=25.877109 204.315111 79.269959 252.823984]{spiral_1}
\end{document}
Cheers,

Tomek
steve6390
Posts: 16
Joined: Sat Nov 29, 2008 6:36 am

Re: avoiding viewport when using includegraphics with pdf files

Post by steve6390 »

Hi Tomek -- Thanks for the pointer. I tried your sample code. It built fine, but the displayed image appeared to be normal letter size with the small sprial shape in the upper left. Maybe I misunderstand?

In the past I tried clipping a pdf file with gs (gswin32c in my case). However, I'm unable to find a solution that actually works without the viewport when included in my .tex files. The viewport is always necessary.

I'm trying again with pdfcrop, which failed with windows + Activestate PERL. Upgrading my cygwin install in hopes that will work.
User avatar
T3.
Posts: 208
Joined: Fri Mar 14, 2008 12:58 pm

Re: avoiding viewport when using includegraphics with pdf files

Post by T3. »

Works fine here and shows correctly (i.e. page is cropped to the viewport size) using the exact same file you attached.

Just now I tried it again with MiKTeX 2.7 & TL2008 and it looks OK in arco, foxit, pdf-xchange, sumatra and gsview. Have you tried the code as I posted it? Note that preview package have to be made active to do its magic. Otherwise, the document is typeset as usual.

Cheers,

Tomek
Post Reply