mquinson / po4a

Maintain the translations of your documentation with ease (PO for anything)
http://po4a.org/
GNU General Public License v2.0
121 stars 58 forks source link

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

Open MayeulC opened 1 year ago

MayeulC commented 1 year ago

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 commented 1 year ago

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 commented 1 year ago

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}
}
mquinson commented 1 year ago

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 );
MayeulC commented 1 year ago

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 :)

mquinson commented 1 year ago

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.

MayeulC commented 1 year ago

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.