I would really like to do something like the following:

\includegraphics{\StrSubstitute{No Spaces}{ }{}}

However, this gives me an Undefined control sequence error. I saw some other posts about includegraphics not evaluating macros in arguments. So, I tried this:



\imageFoo{\StrSubstitute{No Spaces}{ }{}}


This also gives me the Undefined control sequence error.

To test further, I defined:


The following then all work as expected:

\StrSubstitute{No Spaces}{ }{}

Can anyone help me understand why includegraphics never works with StrSubstitute?

1 Answer 1


The argument has to expand to the filename.

so you can go


as \foo expands to file.png

But you can not go


as the definition of the command is an assignment and not expandable. The string functions you are invoking are of this form, making internal definitions and not simply expanding to the result.


\newcommand\removespace[1]{\zap@space#1 \@empty}

you could go

\includegraphics{\removespace{ my file}}

to input myfile.png

  • Thanks! This sounds like there isn't a good workaround for this problem, is that right?
    – Kyle
    May 28, 2014 at 16:23
  • 2
    @Kyle you can either use xstring to define a macro with the spaces removed (I think) and then use that macro in includegraphics, or use a macro such as \zap@space (defined in the latex format) that removes spaces by expansion, or you could just not enter spaces in the first place:-) or you could surround by " and have spaces in the filename or.... May 28, 2014 at 16:27
  • \zap@space seems like the easiest option to use, but it is unrecognized. Is this not included in PDFLaTeX?
    – Kyle
    May 28, 2014 at 18:16
  • 1
    @Kyle probably you didn't make a @ a letter I'll add something to the answer May 28, 2014 at 18:54

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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