Document ClassesStrings w/ underscores passed strings \includegraphics

Information and discussion about specific document classes and how to create your own document classes.
Post Reply
Gnosos
Posts: 23
Joined: Wed Jun 29, 2011 10:52 pm

Strings w/ underscores passed strings \includegraphics

Post by Gnosos »

I am trying to make a package that provides a command to insert a graphics file having a complex name but the user only has to specify a pre-defined short synonym for the file.

For example, suppose we have several files with names like "Hello_World_9_375_4C_27.pdf." I want either to create a new command or to use the \insertgraphics command from the graphicx package. But instead of having to specify the full file name of the graphic, the user would only need to give an abbreviation. For example, instead of entering "\insertgraphics{Hello_World_9_375_4C_27.pdf}" the user would just have to enter something like "\graphic{HW}". Here "HW" stands for "Hello_World," etc., and there would be several abbreviations for the various graphics.

I'm having two problems. The first is that LaTeX does not like strings with underscores. I think I can fix this by escaping all the underscores, as in "Hello\_World\_9\_375\_4C\_27.pdf"

The second problem is that I cannot get the expanded abbreviation to work as an argument to \includegraphics{}. I'm not yet at the point of putting this into a package, so I'm just testing by putting the statements defining the abbreviations in the document preamble and the statements using them in the document body. I've tried two different packages to define the abbreviations, Josselin Noirel's "clefval" and Matt Swift's "abbrevs" package.

Clefval creates a hash with something like \TheKey{HW}{Hello_World_9_375_4C_27.pdf}, while abbrevs creates an abbreviation with \newabbrev{HW}{Hello_World_9_375_4C_27.pdf}. Then to use them in the document, in clefval one uses \TheValue{HW} or in abbrevs \HW. The problem is that when I use either as arguments to \includegraphics (e.g, \includegraphics{\TheValue{HW}} or \includegraphics{\HW}, the graphic file is not included. I suspect the problem is that graphicx is looking for the graphics file before the abbreviation has been expanded. I tried using \expandafter but had no luck.

I'd really appreciate the help of one of you LaTeX wizards. Thanks.

Recommended reading 2024:

LaTeXguide.org • LaTeX-Cookbook.net • TikZ.org

NEW: TikZ book now 40% off at Amazon.com for a short time.

And: Currently, Packt sells ebooks for $4.99 each if you buy 5 of their over 1000 ebooks. If you choose only a single one, $9.99. How about combining 3 LaTeX books with Python, gnuplot, mathplotlib, Matlab, ChatGPT or other AI books? Epub and PDF. Bundle (3 books, add more for higher discount): https://packt.link/MDH5p

kaiserkarl13
Posts: 707
Joined: Tue Mar 25, 2008 5:02 pm

Strings w/ underscores passed strings \includegraphics

Post by kaiserkarl13 »

Code: Select all

\begin{document}
  \newcommand*{\HW}{Hello_World_9_375_4C_27.pdf}
  \includegraphics{\HW}
\end{document}
The above code seems like it would work (I tested it with a file I actually HAVE on my system). Is there a reason this won't work?

It even works like this:

Code: Select all

\begin{document}
  \newcommand*{\HW}{Hello_World}
  \includegraphics{\HW _9_375_4C_27.pdf}
\end{document}
Post Reply