0

I wanted do define a simple macro like the following:

\newcommand{\testcommand}[1]{
    \includegraphics[]{pictures/#1.jpeg}
}

But this does not work because it adds a space between the argument #1 and .jpeg!
For example: \testcommand{figure} gives me the error File `pictures/figure .jpeg' not found because the file is called figure.jpeg.

Is there a way to fix this without having to add a space in the file name?

Many thanks for your reply.

2 Answers2

0

Turns out I wrote a to a new line:

\testcommand{figure
    }

The space was therfore a tab char!

  • 2
    No, the space was the unescape line ending after figure, not the tab. Just as there will be an additional space in front and after your image because of the line endings in the definition of your macro. – samcarter_is_at_topanswers.xyz Jan 14 '23 at 21:36
  • 2
    end of line makes a space, not a tab. (You also have two unwanted spaces in the definition you show in the question) – David Carlisle Jan 14 '23 at 21:39
0

You have two unwanted spaces in your definition:

\newcommand{\testcommand}[1]{% <--- here
    \includegraphics[]{pictures/#1.jpeg}% <--- here
}

An endline always counts as a space. On the other hand, spaces (and tabs) are ignored at the beginning of a line.

If you also type in

\testcommand{figure
    }

you add a further space.

The two unwanted space in your definition only affect spacing in the document, whereas the one in the call affects the file name, because it's the same as if you called

\textcommand{figure }

But the space doesn't come from the space or tab before }, as you seem to believe, rather from the endline after figure.

Since TeX is a typesetting system, it's obviously sensitive to spaces, because they're an important ingredient for printed text.

Important references:

What is the use of percent signs (%) at the end of lines? (Why is my macro creating extra space?)

When is it harmful to add percent character at end of lines in a \newcommand, or similar

egreg
  • 1,121,712