GeneralMacros in includegraphics options

LaTeX specific issues not fitting into one of the other forums of this category.
Post Reply
olly78
Posts: 14
Joined: Wed Sep 24, 2008 10:57 am

Macros in includegraphics options

Post by olly78 »

Hi all

Sometimes I want a figure which zooms in on a certain region of an image. I do this by:

\begin{figure}
\includegraphics[height=\textwidth,clip,trim=24px 86px 146px 57px]{imname}
\caption{A zoom}
\end{figure}

This works fine. Often I'll want to repeat the zoom (i.e. use the same values for 'trim') across several figures. I'd like to be able to save the values for 'trim' in a macro, so I can define them just once, and change them easily. However, doing the following doesn't work:

\newcommand{\trimvals}{24px 86px 146px 57px}
\begin{figure}
\includegraphics[height=\textwidth,clip,trim=\trimvals]{imname}
\caption{A zoom}
\end{figure}

I have tried many variations on the theme, none of which work - I get a compile error and the image is missing. Does anyone know how I can use a macro here?

Many thanks,
Olly

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

localghost
Site Moderator
Posts: 9202
Joined: Fri Feb 02, 2007 12:06 pm

Macros in includegraphics options

Post by localghost »

The unit px is not a valid unit for LaTeX. You can try first to omit the units in your new command \trimvals. The values will be given the unit bp (Big Point) internally.


Best regards
Thorsten¹
olly78
Posts: 14
Joined: Wed Sep 24, 2008 10:57 am

Macros in includegraphics options

Post by olly78 »

localghost wrote:The unit px is not a valid unit for LaTeX. You can try first to omit the units in your new command \trimvals. The values will be given the unit bp (Big Point) internally.
Thanks. However, I still get the same results with no units as with px - i.e. the crop/zoom works as expected without the macro, but is broken with the macro.
User avatar
Juanjo
Posts: 657
Joined: Sat Jan 27, 2007 12:46 am

Macros in includegraphics options

Post by Juanjo »

Consider the following MWE:

Code: Select all

\documentclass[a4paper]{article}
\usepackage[margin=1.5cm]{geometry}
\usepackage{graphicx}

\makeatletter
\define@key{Gin}{Trim}
           {\let\Gin@viewport@code\Gin@trim\expandafter\Gread@parse@vp#1 \\}
\makeatother

\newtoks\TrimVal

\begin{document}

\includegraphics[Trim=10 20 30 40,clip,height=0.2\textheight]{lion_orig}

\vfill
\newcommand{\trimval}{10 20 30 40}
\includegraphics[Trim=\trimval,clip,height=0.2\textheight]{lion_orig}

\vfill
\TrimVal={10 20 30 40}
\includegraphics[Trim=\the\TrimVal,clip,height=0.2\textheight]{lion_orig}

\vfill
\TrimVal={10mm 20mm 30mm 40mm}
\includegraphics[Trim=\the\TrimVal,clip,height=0.15\textheight]{lion_orig}

\end{document}
Replace lion_orig by your own graphic or download it following the link in my signature. The trick is to use a new key for \includegraphics, say Trim, whose definition is almost identical to that of trim. I've only added the \expandafter command. Then you can pass values by three ways:
  • directly, as you do for trim,
  • by defining a macro with \newcommand (or \def and like);
  • by providing a list of tokens; the register to save the list is defined by \newtoks in the preamble.
Look at the first three \includegraphics to see these methods and their different syntax. If no unit is provided, it is understood that LaTeX should use points (pt). The last \includegraphics just shows the use of different units. I attach the PDF file that results from typesetting the above code.
Attachments
pru.pdf
(29.05 KiB) Downloaded 467 times
The CTAN lion is an artwork by Duane Bibby. Courtesy of www.ctan.org.
olly78
Posts: 14
Joined: Wed Sep 24, 2008 10:57 am

Macros in includegraphics options

Post by olly78 »

Juanjo, that is immensely helpful, and just what I wanted. Thank you so much. It works brilliantly.

For completeness, I include the much less elegant hack I had just managed to work out before I read yours:

Code: Select all

\newlength{\trimllx}
\newlength{\trimlly}
\newlength{\trimurx}
\newlength{\trimury}

\newcommand{\trimvals}{\setlength{\trimllx}{24bp}\setlength{\trimlly}{86bp}\setlength{\trimurx}{146bp}\setlength{\trimury}{57bp}}

\begin{figure}
\trimvals
\includegraphics[height=\textwidth,clip,trim=\trimllx{} \trimlly{} \trimurx{} \trimury]{imname}
\caption{A zoom}
\end{figure}
jsjuni
Posts: 2
Joined: Sun Jul 26, 2009 11:59 pm

Macros in includegraphics options

Post by jsjuni »

I'm trying to do something similar for the 'angle' key. I want to compute the rotation angle in a macro. The example below is a bare skeleton, but it doesn't work.

Code: Select all

\documentclass{article}
\usepackage{graphicx}
\usepackage{calc}
\usepackage{ifthen}
\begin{document}
% this works
\includegraphics[angle=90]{test.eps}
% this works
\newcommand{\myangle}{90}
\includegraphics[angle=\myangle]{test.eps}
% this doesn't work
\renewcommand{\myangle}{\ifthenelse{1=1}{90}{0}}
\includegraphics[angle=\myangle]{test.eps}
\end{document}
LaTeX complains with

Code: Select all

! Illegal parameter number in definition of \@tempa.
<to be read again> 
                   }
l.13 \includegraphics[angle=\myangle]{test.eps}
The definition of the 'angle' key is

Code: Select all

\define@key{Gin}{angle}
           {\Gin@esetsize
            \@tempswatrue
            \edef\@tempa{\toks@{\noexpand\Gin@erotate{#1}{\the\toks@}}}%
            \@tempa}
It's not clear where to insert an \expandafter. Any suggestions?
User avatar
gmedina
Posts: 2313
Joined: Wed Jul 11, 2007 11:45 pm

Macros in includegraphics options

Post by gmedina »

Hi,

I'm not sure about your goal, but using:

Code: Select all

\ifthenelse{1=1}{\renewcommand{\myangle}{90}}{\renewcommand{\myangle}{0}}
instead of

Code: Select all

\renewcommand{\myangle}{\ifthenelse{1=1}{90}{0}}
should work.
1,1,2,3,5,8,13,21,34,55,89,144,233,...
phi
Posts: 577
Joined: Tue Oct 21, 2008 8:10 pm

Re: Macros in includegraphics options

Post by phi »

\ifthenelse is not expandable, so you cannot use it inside the definition of \myangle. Generally I think the ifthen package should be avoided because in most cases there are alternatives that are simpler and have fewer negative side effects. In your case, you have to rewrite your test using TeX primitives like \ifnum.
jsjuni
Posts: 2
Joined: Sun Jul 26, 2009 11:59 pm

Macros in includegraphics options

Post by jsjuni »

Thanks, gmedina and phi, for the replies. I wrote a macro using calc and ifthenelse to set \epsangle, \epswidth, \epsheight, etc, and used those as

Code: Select all

\includegraphics[angle=\epsangle, width=\epswidth, ...]
A bit ugly, perhaps, but it works. Thanks again.
Post Reply