Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Expand \FV@EscChars to handle comma, possibly other characters #15

Closed
gpoore opened this issue Jul 5, 2022 · 4 comments
Closed

Expand \FV@EscChars to handle comma, possibly other characters #15

gpoore opened this issue Jul 5, 2022 · 4 comments

Comments

@gpoore
Copy link
Owner

gpoore commented Jul 5, 2022

\edef\FV@commachar{\string,}
\newcommand{\FV@EscChars}{%
  \let\#\FV@hashchar
  \let\%\@percentchar
  \let\{\@charlb
  \let\}\@charrb
  \let\$\FV@dollarchar
  \let\&\FV@ampchar
  \let\_\FV@underscorechar
  \let\\\@backslashchar
  \let~\FV@tildechar
  \let\~\FV@tildechar
  \let\[\FV@leftsquarebracket
  \let\]\FV@rightsquarebracket
  \let\,\FV@commachar
}
@muzimuzhi
Copy link

Perhaps even a hook so users can add their own?

\appto\FV@EscChars{\FV@EscChars@user}

% definition
\define@key{FV}{escape chars}{\def\FV@EscChars@user{#1}}
\define@key{FV}{escape chars*}{\appto\FV@EscChars@user{#1}}
\fvset{escape chars}
Just for fun, here's an implementation of a more user-friendly option new escape char=<char> | \<char> in latex3.
\documentclass{article}
\usepackage{fvextra}

\begin{document}
\makeatletter
% new escape char = <char> | \<char>
\define@key{FV}{new escape char}{%
  \FV@EscChars@add{#1}%
}
\def\FV@EscChars@user{}

\appto\FV@EscChars{\FV@EscChars@user}


\ExplSyntaxOn
\cs_new_protected:Npn \FV@EscChars@add #1
  {
    \tl_if_single_token:nTF {#1}
      {
        \token_if_cs:NTF #1 
          { \exp_args:Ne \FV@EscChars@@add { \cs_to_str:N #1 } }
          { \exp_args:Ne \FV@EscChars@@@add { \tl_to_str:n {#1} } }
      }
      % #1 contains multiple tokens
      { \msg_error:nnx {fvextra} {invalid-escape-char} { \tl_to_str:n {#1} }}
  }

\cs_new_protected:Npn \FV@EscChars@@add #1
  {
    \exp_args:Ne \tl_if_single_token:nTF { #1 }
      { \FV@EscChars@@@add {#1} }
      % #1 of \FV@EscChars@add is a control sequence but has its csname longer than one
      { \msg_error:nnn {fvextra} {invalid-escape-char} { #1 } }
  }

\cs_new_protected:Npn \FV@EscChars@@@add #1
  {
    % \def \FV@char@<char> { <char> }
    \tl_set:cn { FV@char@#1 } { #1 }
    % append \tl_set_eq:NN \<char> \FV@char@<char> to \FV@EscChars@user
    \exp_args:NNe \tl_put_right:Nn \FV@EscChars@user 
      { \tl_set_eq:NN \exp_not:c {#1} \exp_not:c {FV@char@#1} }
  }

\msg_new:nnn {fvextra} {invalid-escape-char}
  {
    Invalid~ value~ "#1"~ for~ "new~ escape~ char", \\
    should~ be~ either~ a~ single~ char~ or~ length~ 1~ control~ sequence
  }
\ExplSyntaxOff

\fvset{
  new escape char={,}, new escape char=^,
  new escape char=\%, new escape char=\ , new escape char=\#, 
  % should error
  new escape char={:;}, new escape char={\ab}
}

%\ttfamily
%\meaning\FV@EscChars@user
\show\FV@EscChars@user
\makeatother
\end{document}

@gpoore gpoore closed this as completed in 16de52d Nov 10, 2022
@muzimuzhi
Copy link

Resolving of this issue made it necessary to use escaped comma (\,) in breakbefore and breakafter. It that an expected change?

@gpoore
Copy link
Owner Author

gpoore commented Oct 2, 2024

@muzimuzhi Using a comma seems to work as expected:

\documentclass{article}

\usepackage{fvextra}

\begin{document}

TEXT

\begin{Verbatim}[breaklines,breakafter={,}]
a,a,a,a,a,a,a,a,a,a,a,a,a,a,a,a,a,a,a,a,a,a,a,a,a,a,a,a,a,a,a,a,a,a,a,a,a,a,a,a,a,a,a,a,a,a,a,a,a,a,a,a,a,a,a,a,a,a,a,a,a,a,a,a,a,a,a,a,a,a,
\end{Verbatim}

\end{document}

Or is this related to minted, before you created gpoore/minted#407?

@muzimuzhi
Copy link

muzimuzhi commented Oct 2, 2024

Or is this related to minted, before you created gpoore/minted#407?

Ah yes. I made a mistake by testing with breakafter=,, but not breakafter={,}.

Then maybe words "should be backslash-escaped" in the related doc are too strong:

Special characters given to breakafter should be backslash-escaped (usually #,
{, }, %, [, ], and the comma ,; the backslash \ may be obtained via \\ and the
space via \space).

muzimuzhi referenced this issue in T-F-S/tcolorbox Oct 2, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants