4

How to include graphs with filename starting with # sign? Say I have an image #1-1.png. The following error message will pop up when I try to include it.

Package pdftex.def Error: File `##1-1.png' not found. \includegraphics[scale=0.7]{#1-1.png}*
cfr
  • 198,882
Harry
  • 41
  • 1
    Welcome! Just rename the file. # is a special character. Life will be infinitely easier if you rename the image as opposed to trying to persuade TeX to load a file containing weird and wonderful characters. It is just like avoiding spaces in file names. Even when it is possible to use extreme methods to force TeX to accept them, it is much, much easier just to use proper file names. – cfr Apr 27 '16 at 00:37
  • Yeah sure it's easy to rename one or two images but if you have hundreds/thousands of them... – Harry Apr 27 '16 at 01:40
  • If you have even tens of them - never mind hundreds or thousands - you tell your computer to rename them for you using a simple script. Or you just use detox which saves even that trouble but may be a little less customisable. At least, that's what I would do/do do/have done. – cfr Apr 27 '16 at 01:52
  • 1
    http://tex.stackexchange.com/questions/294765/file-path-with-symbol-causes-error/294791#294791 – Ulrike Fischer Apr 27 '16 at 07:18

1 Answers1

2

As you discovered, # is a special character for TeX. If you don't plan to need things like

\includegraphics[...]{./subdir/#1-1.png}

but you just type the file name, you should be able to do with a new command:

\makeatletter
\edef\harry@hash{\string#}
\newcommand{\hashincludegraphics}[2][]{%
  \includegraphics[#1]{\harry@hash#2}%
}
\makeatother

and use

\hashincludegraphics[scale=0.7]{1-1.png}

However, my advice is to rename those files.

egreg
  • 1,121,712