1

I am not able to compile the following document with pdflatex.

\documentclass{article}    
\usepackage{graphicx}    
    
\NewDocumentCommand{\logoscale}{}{0.3}    
%\newcommand{\logoscale}{0.3}  <---- works with this  
    
\NewDocumentCommand{\mylogo}{}%    
  {\includegraphics[scale=\logoscale]{example-image-a}}                                                                         
                                                                                                                                
\begin{document}                                                                                                                
                                                                                                                                
\mylogo                                                                                                                         
                                                                                                                                
Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy                                                        
eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam                                                        
voluptua. At vero eos et accusam et justo duo dolores et ea rebum. Stet                                                         
clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit                                                         
amet.                                                                                                                           
                                                                                                                                
\end{document}

On compiling, I get the following error:

./x.tex:21: pdfTeX error (\pdfsetmatrix): Unrecognized format..
<argument> ...shipout:D \box_use:N \l_shipout_box 
                                                  \__shipout_drop_firstpage_...
l.21 \end{document}

The same document compiles if I use newcommand. I need some help in understanding where the problem is.

12
  • 2
    Try with \NewExpandableDocumentCommand{\logoscale}{}{0.3} Commented Oct 25, 2024 at 14:51
  • 4
    Just for holding a value, there is no real reason to use the \New... macros. Commented Oct 25, 2024 at 14:56
  • 2
    @wipet Indeed I prefer \def myself because it is the simplest solution. However, it is not without problems, the biggest one is that you can easily overwrite an earlier defined macro unintentionally because (La)TeX doesn't warn about redefinitions with \def. Commented Oct 25, 2024 at 17:39
  • 4
    This question is similar to: Using command defined with \NewDocumentCommand in TikZ. If you believe it’s different, please edit the question, make it clear how it’s different and/or how the answers on that question are not helpful for your problem. It may seem that the similar question is completely different because it is about TikZ instead of \includegraphics. However, the expansion problem and its solution are actually the same. Commented Oct 26, 2024 at 8:20
  • 1
    @Teepeemm Depending on the context graphicx uses \edef internally when processing scale. And there \logoscale is not expanded, because \NewDocumentCommand defines it protected. And in another context, graphicx uses the argument of scale as a prefix factor to a length. This also needs an expandable macro. So expansion is the problem. Commented Oct 27, 2024 at 19:15

0

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.