TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I'm inserting graphics from data in a csv-file and due to lazy input I have to get rid of leading and trailing spaces. It works fine with the trimspaces package.


\def\testA{example-image-a }
%\includegraphics{\testA} %error

\def\testB{ example-image-a}
%\includegraphics{\testB} %error




By curiosity I tried to get this with expl3 and \tl_trim_spaces:n but didn't find a sensible way to store the content of the commands without the spaces in a command. Does someone know how to do it?

share|improve this question
up vote 7 down vote accepted

Two ways. With the first method you use \trimspaces in front of the macro; with the second you can ‘normalize’ a macro to have leading and trailing spaces removed.


  \tl_trim_spaces:V #1
\cs_generate_variant:Nn \tl_trim_spaces:n { V }

  \tl_trim_spaces:N #1


\def\testA{example-image-a }
\def\testB{ example-image-a}







enter image description here

share|improve this answer
Ah I see. I hadn't understand that \tl_trim_spaces:N does the same as \trim@spaces@in, I thought it would leave things in the input stream like \tl_trim_spaces:n. And I hadn't thought about V type, I only tried x. – Ulrike Fischer Dec 19 '13 at 10:53
@UlrikeFischer The description of \tl_trim_spaces:N could be clearer. – egreg Dec 19 '13 at 10:56
I can look to improve the docs for a release likely next week: @UlrikeFischer was there something specific that was missing? – Joseph Wright Dec 19 '13 at 11:23
@JosephWright Maybe “Resets ⟨tl var⟩ by removing any leading and trailing explicit space characters (explicit tokens with character code 32 and category code 10) from its content.” – egreg Dec 19 '13 at 11:26
@JosephWright: The main source of confusion was in my case the naming of the commands: Both \tl_trim_spaces:n and \tl_trim_spaces:N share the same command "body" \tl_trim_spaces and so I assumed that they are variants and do more or less the same only on different types of arguments and I transfered "leaves the result in the input stream" from the description of \tl_trim_spaces:n to the other command. – Ulrike Fischer Dec 19 '13 at 15:30

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.