General ⇒ Macros in includegraphics options
Macros in includegraphics options
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
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
NEW: TikZ book now 40% off at Amazon.com for a short time.

- localghost
- Site Moderator
- Posts: 9202
- Joined: Fri Feb 02, 2007 12:06 pm
Macros in includegraphics options
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¹
Best regards
Thorsten¹
How to make a "Minimal Example"
Board Rules
Avoidable Mistakes
¹ System: TeX Live 2025 (vanilla), TeXworks 0.6.10
Board Rules
Avoidable Mistakes
¹ System: TeX Live 2025 (vanilla), TeXworks 0.6.10
Macros in includegraphics options
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.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.
Macros in includegraphics options
Consider the following MWE:
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:
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}
- 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.
- Attachments
-
- pru.pdf
- (29.05 KiB) Downloaded 472 times
The CTAN lion is an artwork by Duane Bibby. Courtesy of www.ctan.org.
Macros in includegraphics options
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:
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}
Macros in includegraphics options
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.
LaTeX complains with
The definition of the 'angle' key is
It's not clear where to insert an \expandafter. Any suggestions?
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}
Code: Select all
! Illegal parameter number in definition of \@tempa.
<to be read again>
}
l.13 \includegraphics[angle=\myangle]{test.eps}
Code: Select all
\define@key{Gin}{angle}
{\Gin@esetsize
\@tempswatrue
\edef\@tempa{\toks@{\noexpand\Gin@erotate{#1}{\the\toks@}}}%
\@tempa}
Macros in includegraphics options
Hi,
I'm not sure about your goal, but using:
instead of
should work.
I'm not sure about your goal, but using:
Code: Select all
\ifthenelse{1=1}{\renewcommand{\myangle}{90}}{\renewcommand{\myangle}{0}}
Code: Select all
\renewcommand{\myangle}{\ifthenelse{1=1}{90}{0}}
1,1,2,3,5,8,13,21,34,55,89,144,233,...
Re: Macros in includegraphics options
\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.
Macros in includegraphics options
Thanks, gmedina and phi, for the replies. I wrote a macro using calc and ifthenelse to set \epsangle, \epswidth, \epsheight, etc, and used those asA bit ugly, perhaps, but it works. Thanks again.
Code: Select all
\includegraphics[angle=\epsangle, width=\epswidth, ...]