latex2e icon indicating copy to clipboard operation
latex2e copied to clipboard

[ltcmd] Verbatim command breaks with backslash as a delimiter

Open PhelypeOleinik opened this issue 3 years ago • 11 comments

Brief outline of the bug

Spotted here, a command defined with a v argument breaks when a catcode 0 character is used as delimiter for the argument, as in \verb\foo\ (which is valid for the standard LaTeX \verb).

Either the diagnostics should be fixed to throw an appropriate error about \ not being possible as a delimiter, or the scanner should be fixed to allow \ as \verb does.

Minimal example showing the bug

\RequirePackage{latexbug}
\documentclass{article}
\NewDocumentCommand{\myverb}{v}{\texttt{#1}}
\def\foo{wrong}
\begin{document}
\verb\foo\

\myverb\foo\
\end{document}

Log file (required) and possibly PDF file

test.log

Explanation for the current behaviour

The v scanner starts (\__cmd_grab_v_aux:w) by using \peek_remove_spaces:n, which uses \futurelet in \foo, freezing it as a control sequence, then checks if the next token is a {, which is false, and then checks if the next token is an N-type, which is true. At this point, the next token should be a character, so \__cmd_grab_v_aux_test:N checks \foo for that, which is not, thus calling \__cmd_grab_v_aux_abort:n to raise an error. \__cmd_grab_v_aux_abort:n then assumes that, since it saw a non-character token, that the verbatim command was misused in an argument.

PhelypeOleinik avatar Jul 14 '21 02:07 PhelypeOleinik

\verb does not only accept \ it also accepts %, {, or } all of which make very weird input and all of which give strange diagnostics with "v"

\documentclass{article}

\NewDocumentCommand\baz{v}{\texttt{#1}}

\begin{document}

\typeout{All working}

\verb\foo\

\verb{foo{

\verb}foo}

\verb%foo%

\typeout{Not working}

\baz%foo%

\baz}foo}

\baz{foo{

\baz\foo\

\end{document}

I'm tempted to consider the \verb behavior unsupported and and even disallow it going forward (even if that may break a very small number of documents). Nevertheless the diagnostics given by \NewDocumentCommand are something we should fix for all cases.

FrankMittelbach avatar Jul 14 '21 07:07 FrankMittelbach

I tend to agree that use of these should be deprecated with \verb etc. : \ { } (catcode < 3 ).

^ (catcode 7) is I think (like %, catcode ) a bit tricky to document its behaviour, due to ^^ (etc)-notation; so perhaps these two should join the list of unwise choices.

car222222 avatar Jul 15 '21 10:07 car222222

This issue has been automatically marked as stale because it has not had recent activity.

stale[bot] avatar Sep 19 '21 01:09 stale[bot]