General ⇒ Macros in includegraphics options
Macros in includegraphics options
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.
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
Best regards
Thorsten¹
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
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 467 times
Macros in includegraphics options
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
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
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}}
Re: Macros in includegraphics options
Macros in includegraphics options
Code: Select all
\includegraphics[angle=\epsangle, width=\epswidth, ...]