This problem is driving me nuts. I'd like to change the includegraphics path depending on some conditions. It seems simple, right?
I already read the answers to this question: Passing image path to \includegraphics using a macro, but I was unable to solve my issue. I learned that for \includegraphics the following must be true:
- The path must be expandable (requires
\edefor cannot be protected therefore no xparse unless\DeclareExpandableDocumentCommandis relevant?) https://tex.stackexchange.com/a/120972/13552 - includegraphics only supports one level of expansion. https://tex.stackexchange.com/a/120975/13552
Thanks David Carlisle -> Incorrect Statement: From my understanding, my code only requires one level of expansion, but is still not working.
Is this a rare time when I need to use \DeclareExpandableDocumentCommand for \qikpicpath?
Code
A prerequisite is having a jpg file on directory above the tex file called test_pic.jpg. I commended out the problematic code.
\documentclass{article}
\usepackage{fontspec}
\newcommand{\qikrootdirectory}{../}
\usepackage{xparse}
\usepackage{xstring}
\usepackage{tikz}
\ExplSyntaxOn % https://tex.stackexchange.com/a/275552/13552
\tl_new:N \g_qik_pic_class_tl % allocate a (global) variable
\tl_gset:Nn \g_qik_class_tl { I } % initialize the value
\NewDocumentCommand{\qikpicclass}{} % define a user level command for printing the value
{
\tl_use:N \g_qik_class_tl
}
\NewDocumentCommand{\qiksetpicclass}{m} % define a user level command for changing the value
{
\tl_gset:Nn \g_qik_class_tl { #1 }
}
\NewDocumentCommand{\qikpicpath}{}
{
\tl_if_eq:VnTF \g_qik_class_tl { I }
{ \qikrootdirectory~test_pic.jpg } % true
{ \qikrootdirectory~test_pic.jpg } % false
}
\cs_generate_variant:Nn \tl_if_eq:nnTF { V }
\ExplSyntaxOff
\begin{document}
\qikpicpath % Visually inspect expanded text.
\includegraphics[width=\textwidth]{\qikrootdirectory test_pic.jpg}% Works
%\includegraphics[width=\textwidth]{\qikpicpath}% Expansion Problem
\tikz \node (pic) {\includegraphics[width=\textwidth]{../test_pic.jpg}};% Works
%\tikz \node (pic) {\includegraphics[width=\textwidth]{\qikpicpath}};% Expansion Problem Related to Above
\end{document}
~space beforetest_pic! – egreg Oct 29 '15 at 15:33~spaces, but my output after uncommenting the first commented out line is! You can't use \the letter I' after \the.\def\qikrootdirectory{../}should work if../works (security settings intexmf.cnfmight prevent reading files in a path starting../) – David Carlisle Oct 29 '15 at 16:50\includegraphics>\qikpicpath>\tl_if_eq:VnTF \g_qik_class_tl { I }>\jkorootdirectory? Is that right? How is it not clear what problem I am trying to solve? The path is most definitely required in my case. The entire SVN system depends on paths. – Jonathan Komar Oct 29 '15 at 16:56article.clsin every document for example. If TeX can find article.cls it can find your jpg files. – David Carlisle Oct 29 '15 at 17:03/usr/bin/texlive. If multiple documents in separate dirs under a common directory structure must access the same picture, it makes sense to have them located higher in the tree and shared amongst docs (save resources). Multiple users can check out a fully working dir tree from the revision control system without admin rights and everything should compile. How else would I tell LaTeX where the global objects are (objs. because there are other global things). – Jonathan Komar Oct 29 '15 at 17:13\graphicspath) and then\includegraphics{test_pic}will work. – David Carlisle Oct 29 '15 at 17:17\includegraphics. – Jonathan Komar Oct 29 '15 at 17:32\graphicspathdoesn't do what you need why do you have to hide the filename in a macro as you comment there is no problem using a macro top hold the path. – David Carlisle Oct 29 '15 at 17:52\graphicspath, that could be an option if\graphicspath{\qikrootdirectory}would work. There is a caveat to this method, however. It will introduce a constraint on the filenames. No local image may have the same name as a global image. I do not know why anybody would intentionally do such a thing, but it would be a restriction that I currently do not have. – Jonathan Komar Oct 29 '15 at 18:45\graphicspath{{\qikrootdirectory}}– David Carlisle Oct 29 '15 at 22:23