3

A recent MikTeX update broke some code that I'm using. I have many files filled with filenames, the code reads these filenames, and then does some stuff with the contents of the corresponding file. The contents of each file are some combination of (1) balanced groups of text/code which is printed, or (2) filenames on which the process is repeated.

I compile with lualatex.

The issue is that since my update, files cannot be found because of an end of line character that is appended to the file extension. I could use \ior_str_map_inline: but since some of the filenames are commented, I then get file not found errors due to the % at the start of the filename. Even if those weren't commented, the combination of code and filenames in some of the files would mean that if everything were "stringified", then the code won't compile. I also tried to read as a string, and then to convert back a token list using \scantokens and \scantextokens but what I tried did not work.

Is there an easy way to make things just work again?

p.s. The filenames do indeed contain underscores.

\documentclass{article} \usepackage{xparse} \begin{filecontents*}{files.tex} file_1.tex %file_2.tex elif_3.tex \end{filecontents*} \ExplSyntaxOn \NewDocumentCommand{\getstuff}{m} { \file_if_exist:nT {#1} { \ior_open:Nn \g_tmpa_ior {#1} \ior_map_inline:Nn \g_tmpa_ior { % if a filename has been commented, then ##1 will be empty \tl_if_empty:nTF {##1} {\tl_show:n {"##1"~is~empty!}} { % check whether file exists \file_if_exist:nTF {##1} { % and branch depending on whether filename contains x \tl_if_in:nnTF {##1}{file} {\tl_show:n {Success~##1}} {\tl_show:n {Fail~##1}} } {\tl_show:n {"##1"~doesn't~exist!}} } } \ior_close:N \g_tmpa_ior } } \ExplSyntaxOff \begin{document} \getstuff{files} \end{document} 
2
  • 1
    You've found a bug in \pdffilesize here: it's subtle but is why if you try the example with pdfTeX everything says it is found! Commented Oct 18, 2019 at 17:37
  • 1
    The change is because we've altered the file-search approach. What you are seeing with LuaTeX is actually right (and Phelype's answer is the correct fix), but it's perhaps not anticipated. Commented Oct 18, 2019 at 17:41

1 Answer 1

3

What you're seeing is TeX inserting the \endlinechar (which usually is a space character) at the end of the line. You can trim spaces around a token list using \tl_trim_spaces:n or \tl_trim_spaces_apply:nN, whose second argument is a function which will take the space-trimmed token list as argument (another way out would be to set \endlinechar=-1 so that nothing would be inserted).

\documentclass{article} \usepackage{xparse} \begin{filecontents*}{files.tex} file_1.tex %file_2.tex elif_3.tex \end{filecontents*} \ExplSyntaxOn \NewDocumentCommand { \getstuff } { m } { \file_if_exist:nT {#1} { \ior_open:Nn \g_tmpa_ior {#1} \ior_map_inline:Nn \g_tmpa_ior { % if a filename has been commented, then ##1 will be empty \tl_if_blank:nTF {##1} { \iow_term:n {"##1"~is~empty!} } { % check whether file exists \tl_trim_spaces_apply:nN {##1} \file_if_exist:nTF { % and branch depending on whether filename contains x \tl_if_in:nnTF {##1}{file} { \iow_term:n {Success~##1} } { \iow_term:n {Fail~##1} } } { \iow_term:n {"##1"~doesn't~exist!} } } } \ior_close:N \g_tmpa_ior } } \ExplSyntaxOff \begin{document} \getstuff{files} \end{document} 

and the terminal shows:

Success file_1.tex "" is empty! "elif_3.tex" doesn't exist! 
3
  • Thanks a lot Phelype, this works for the MWE and I think I should be able to get it working in my actual code once I figure out where the other errors are coming from :) Commented Oct 18, 2019 at 18:10
  • @ScottH. You're welcome :-) I made other small modifications (replaced \tl_if_empty:n by \tl_if_blank:n and \tl_show:n by \iow_term:n), but essentially I replaced \file_if_exist:nTF {##1} by \tl_trim_spaces_apply:nN {##1} \file_if_exist:nTF. Any problems, feel free to ask :) Commented Oct 18, 2019 at 18:15
  • No problems, have it all up and running. Thanks again. Commented Oct 18, 2019 at 18:42

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.