po4a icon indicating copy to clipboard operation
po4a copied to clipboard

latex: `\input` breaks when surrounded with {curly braces}

Open MayeulC opened this issue 2 years ago • 6 comments

Minimal reproducer (main.tex, with at least an empty subfile.tex next to it):

\documentclass{article} 
 
\begin{document} 
{\input{subfile.tex}}
\end{document}

See that \input is surrounded by curly braces. Compilation fails when that is the case, and succeeds otherwise. As to why I need this: I use multiple tikzpicture graphs in subcaptionbox that I input from other files.

This is the (confusing) error:

Use of uninitialized value $newfilepath in string eq at /usr/share/perl5/vendor_perl/Locale/Po4a/TeX.pm line 998.
po4a::tex: Cannot find main.tex with kpsewhich

Even more confusingly, the exact same error is displayed when subfile.tex does not exist (with or without curly braces), and complains that the parent file cannot be found, while it's the included file that does not exist.

MayeulC avatar Apr 30 '23 19:04 MayeulC

After testing some more, the issue comes from the following regex:

https://github.com/mquinson/po4a/blob/51f9008e7aa3803b76a2b8c7511c5c54dbdf8322/lib/Locale/Po4a/TeX.pm#L970-L972

Which matches the wrong filename (capture group 3), as subfile.tex} (including the curly bracket).

Including the curly bracket in the list of non-matching symbols seems to fix it, and it is highly unlikely to have this in a filename, especially when it comes to TeX source code, so I'll submit a PR.

MayeulC avatar May 01 '23 16:05 MayeulC

I submitted a PR, but there is another issue (besides the error message that I should improve): \input{subfile.pgf} won't work with a pgf extension, weirdly (it works with other random extensions I tried). It displays the same error:

Use of uninitialized value $newfilepath in string eq at /usr/share/perl5/vendor_perl/Locale/Po4a/TeX.pm line 999.
po4a::tex: Cannot find main.tex with kpsewhich

I don't think I'll try to fix that one, I usually use the following snippet for including pgf plots anyway:

\newcommand{\importpgf}[2]{% see https://tex.stackexchange.com/a/640884 include from subfolders with \importpgf{figures/path}{img}
        \tikzsetnextfilename{pgf-#2}%
        \begin{tikzpicture}%
                \node[inner sep=0pt] {\import{#1}{#2.pgf}};
        \end{tikzpicture}
}

MayeulC avatar May 01 '23 17:05 MayeulC

Maybe the following patch fixes the confusing "Use of uninitialized value" part of the error message? Not tested at all.

--- a/lib/Locale/Po4a/TeX.pm
+++ b/lib/Locale/Po4a/TeX.pm
@@ -993,7 +993,7 @@ sub read_file {
 
                 # search the file
                 open( KPSEA, "kpsewhich " . $newfilename . " |" );
-                my $newfilepath = <KPSEA>;
+                my $newfilepath = <KPSEA> // ""; # Use an empty string if the content is undef
 
                 if ( $newfilename ne "" and $newfilepath eq "" ) {
                     die wrap_mod( "po4a::tex", dgettext( "po4a", "Cannot find %s with kpsewhich" ), $filename );

mquinson avatar May 01 '23 23:05 mquinson

Possibly, I'll try that, thank you. It's my first time touching Perl code, though I am comfortable enough with regexes (and... they aren't the best tool for parsers).

I was thinking of checking if it was undef, and displaying an error message containing $newfilename instead of $filename because that's much more useful for the user.

Actually, maybe that's an oversight in the die expression below, and it should read something like "Cannot find %s with kpsewhich, included from %s" ), $newfilename, $filename ) (~~not sure if multiple arguments need to be specified separately or in an array~~ edit: tested, works as-is).

Edit: I suggest doing both. Your suggestion is very good, it avoids the confusing "Use of uninitialized value", I tested it and it works.

However, the current message is still misleading, as it is not $filename that cannot be found, but $newfilename. Hence why I also suggest rewording the message. I may submit another PR later if you want, but feel free to do so :)

MayeulC avatar May 02 '23 08:05 MayeulC

You should post new messages, not edit the old ones : I don't get any notification per email when you edit your messages ;)

If you can prepare a PR, it'd be really appreciated as I'm somewhat overloaded these days. If not, I'll eventually do it at some point, but I feel less confident because testing po4a on latex is not 100% trivial for me right now.

mquinson avatar May 04 '23 22:05 mquinson

You should post new messages, not edit the old ones : I don't get any notification per email when you edit your messages ;)

Okay, I'll do that :)

I have a bit more time these days, I'll try to send another PR for the error message when I can.

MayeulC avatar May 08 '23 07:05 MayeulC