I want make a function, it can insert image and attach file (e.g., the data about plotting), so I need set a variable for image name and it will use for attachfile.(\attachfile from attachfile2 package.)

But now I don't know how to use variable for \includegraphics and \attachfile in LaTeX3.

(I ignored \ExplSyntaxOn and \ExplSyntaxOff in below code.)

Question 1: \includegraphics

Variable for LaTeX2e is fine.


But LaTeX3 failed:

\tl_new:N \l_img_name_tl
\tl_set:Nn \l_img_name_tl {example-image.eps} 
\includegraphics{\tl_use:N \l_img_name_tl}

Successful only when I use "example-image" (no extension), but I want keep extension for select different file which has same name (img.eps, img.png, ....), so I tried another variable for extension but failed also.

\tl_new:N \l_img_name_tl
\tl_set:Nn \l_img_name_tl {example-image} 
\tl_new:N \l_ext_name_tl
\tl_set:Nn \l_ext_name_tl {eps}
\includegraphics{\tl_use:N \l_img_name_tl . \tl_use:N \l_ext_name_tl}

(Update: I discover above code only failed in eps, it work fine in pdf, png, jpg. And \tl_use:N may not required, I am not sure.)

Question 2: \attachfile

LaTeX2e failed also:


Look like \attachfile will not expand any macro. So I define command to fix, Do you have any ideal better than this ? (Below code is LaTeX3)

\NewDocumentCommand{\eftaf}{m}{ % expand filename to \textattachfile
\eftaf{\tl_to_str:N \l_data_fullname_tl}

Question 3: dot in file name

I know when filename include dot, we should use {}. (see here)


Do we have better method to solve this problem in LaTeX3? For example use str type for image name without extension. (And It will split from name with extension.)

Update, because I can use \includegraphics by variable now.str type can work fine. Below is code:

\tl_new:N \l_img_name_tl
\tl_set:Nn \l_img_name_tl {img.img} 
\tl_new:N \l_ext_name_tl
\tl_set:Nn \l_ext_name_tl {pdf} 
\includegraphics{\tl_to_str:N \l_img_name_tl . \l_ext_name_tl}

But \l_img_name_tl can not use {{img.img}}, any ideal for expand this?

  • 3
    The problem with multiple dots in a graphics file name is solved by package grffile. – Heiko Oberdiek Mar 31 '17 at 8:24
  • The file name parser that is used by package graphics requires that the extension is not hidden inside a macro. Only a macro at the start of the image file name is expanded once. – Heiko Oberdiek Mar 31 '17 at 10:33
  • Maybe related: tex.stackexchange.com/questions/350258 – Dr. Manuel Kuehner Apr 6 '17 at 11:40

Your Answer

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

Browse other questions tagged or ask your own question.