I have been trying to write a simple new command to make a piece of code more flexible and readable (a MWE is below). In it I need to be able to pass a string to a new command that contains a hashtag #. I don't have any choice in the matter... I have to use a file that is auto produced and externally maintained which contains #1 or #2 in the file name.

Things I have tried that don't seem to be working:

  • escaping the hashtag in the argument (i.e. {a \#1.jpg}
  • \detokenize{} on the argument #1
  • \detokenize{} on the string passed as an argument







    \Customjpg{a \#1.jpg}{Fig:R1a}

  • 2
    so your file is really called a #1.jpg ? (\string# probably works.) Apr 2, 2016 at 19:57
  • 1
    See tex.stackexchange.com/a/294791/2388 Apr 2, 2016 at 20:03
  • @UlrikeFischer Thanks I have implemented \begingroup \catcode\#=12` and it helps keep the automation of my tex files cleaner than adding the \string. I played around with the possibility of a verbatim command like that suggested in the alternate answer tex.stackexchange.com/a/294804/69378 but I think the local redefinition is the cleanest solution. I would be happy to accept this as an answer. Better yet, if it can be nested into it's own newcommand or environment (that doesn't propagate the same problem by still having to pass the # in a string) I would be most intrigued.
    – EngBIRD
    Apr 2, 2016 at 20:23

2 Answers 2


You can locally change the catcode of the # in your command:


Be aware, that you can't use \Customjpg in the argument of another command.

  • I am nesting this command within a \subfloat{} which is why egreg's answer in the linked question with the verbatim argument didn't work out for me. I could investigate whether it would be appropriate to but the subfloat inside (which I think would get around the "argument of another command problem...)
    – EngBIRD
    Apr 2, 2016 at 20:40
  • @EngBIRD I had overlooked egreg's answer. It does basically the same, only in a bit more modern way. If you really need it inside subfloat better change the catcode of # more globally. Apr 2, 2016 at 20:50
  • At the moment, I have placed it on the outside of my figure environments - is that the kind of "more globally" applied instance you had in mind?
    – EngBIRD
    Apr 2, 2016 at 21:00

You can use

\includegraphics{a \zz1.png}

to get a non-special # (\# is a \chardef token which typesets a # but doesn't expand to a # in filenames etc).

  • That doesn't work for me. \includegraphics{a\string#1} gives ! Illegal parameter number in definition of \@tempb (I mean it works when grffile is used, but not in general) Apr 2, 2016 at 20:25
  • @UlrikeFischer hmm so it does, who wrote that code:-) I fixed, but now it's more or less a duplicate of your answer on the other so perhaps vote to close... Apr 2, 2016 at 20:30
  • 1
    I would be happy to have this question closed or deleted and propose some edits to the linked question because it never came up during searches. While that question already has an accepted answer, I personally found your answer informative and complimentary. If this question is closed or deleted would it be appropriate to add this as a potential answer to the linked question?
    – EngBIRD
    Apr 2, 2016 at 20:38
  • 1
    @EngBIRD well as you say this one has a better title, so perhaps just let it stand. Apr 2, 2016 at 20:53

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .