如果我用v
参数读取逐字字符串,所有换行符都会被替换为标记^^M
(在 latex 中似乎是回车符)。问题是,当我将其写入文件时,它实际上写入的^^M
不是换行符。我尝试用 替换它\tl_replace_all:Nnn {^^M} {^^J}
,但它给出了错误...
这个我无法删除的 ^^M 是什么?我该如何摆脱它?
平均能量损失
\documentclass[]{article}
\begin{document}
%\def^^M{AA}
\ExplSyntaxOn
\str_new:N \__robExt_tmp_contain_code_str
\NewDocumentCommand{\robExtCacheMeCode}{O{}+v}{%
{% Group
%% We store the input in a non-string element for efficiently implementing "auto forward"
\tl_set:Nn \__robExt_tmp_contain_code_str {#2}
% fails with error Runaway argument?
% \tl_replace:Nnn \__robExt_tmp_contain_code_str {^^M} {^^J}
\str_show:N \__robExt_tmp_contain_code_str
\begin{flushleft}\ttfamily%
\__robExt_tmp_contain_code_str
\end{flushleft}%
}
}
\ExplSyntaxOff
\noindent This works:\robExtCacheMeCode{42 % 8}.
\noindent This fails, first because newlines are turned into caret caret M, but also becase characters like :\robExtCacheMeCode{
Hey I am a multi-line text with { } # foo []<> % many caracters, including
newlines like her.
}.
\end{document}
编辑 我尝试应用大卫卡莱尔的建议,我设法将它们变成空格,但没有变成换行符......
尝试 1:
\documentclass[]{article}
\begin{document}
\ExplSyntaxOn
\str_new:N \__robExt_tmp_contain_code_str
\tl_new:N \__robExt_tmp_contain_code_tl
\def\foo{^^J}
\NewDocumentCommand{\robExtCacheMeCode}{O{}+v}{%
{% Group
%% We store the input in a non-string element for efficiently implementing "auto forward"
\tl_set_rescan:Nnn \__robExt_tmp_contain_code_tl {
\char_set_active_eq:nN { `\^^M } \foo
\cctab_select:N \c_other_cctab
} {#2}
\str_set:Ne \__robExt_tmp_contain_code_str {\tl_to_str:e {\__robExt_tmp_contain_code_tl}}
\str_show:N \__robExt_tmp_contain_code_str
\begin{flushleft}\ttfamily%
\__robExt_tmp_contain_code_str
\end{flushleft}%
}
}
\ExplSyntaxOff
\noindent This works:\robExtCacheMeCode{42 % 813}.
\noindent This fails, first because newlines are turned into caret caret M, but also becase characters like :\robExtCacheMeCode{
Hey
I am a multi-line text
with { } # foo []<> % many caracters, including
newlines like here.
}.
\end{document}
尝试2:
\documentclass[]{article}
\begin{document}
\ExplSyntaxOn
\str_new:N \__robExt_tmp_contain_code_str
\tl_new:N \__robExt_tmp_contain_code_tl
\def\foo{^^J}
\NewDocumentCommand{\robExtCacheMeCode}{O{}+v}{%
{% Group
%% We store the input in a non-string element for efficiently implementing "auto forward"
\char_set_active_eq:nN { `\^^M } \foo
\tl_set:Nx \__robExt_tmp_contain_code_tl {#2}
\str_set:Ne \__robExt_tmp_contain_code_str {\tl_to_str:e {\__robExt_tmp_contain_code_tl}}
\str_show:N \__robExt_tmp_contain_code_str
\begin{flushleft}\ttfamily%
\__robExt_tmp_contain_code_str
\end{flushleft}%
}
}
\ExplSyntaxOff
\noindent This works:\robExtCacheMeCode{42 % 813}.
\noindent This fails, first because newlines are turned into caret caret M, but also becase characters like :\robExtCacheMeCode{
Hey
I am a multi-line text
with { } # foo []<> % many caracters, including
newlines like here.
}.
\end{document}
答案1
^^M
是 control-M,字符 13(回车符)。它通常由 TeX 自动添加到输入行的末尾,但如果您在其具有默认 catcode 5 时明确添加它,它的作用与注释非常相似,行的其余部分将被丢弃。不同之处在于它会添加空格。
参数v
类型将行尾记录为 catcode 12,^^M
因此您可以例如用\par
注意使用来替换它们,tl
而不是str
用于可以保存 csname 标记的变量。
\documentclass[]{article}
\begin{document}
%\def^^M{AA}
\ExplSyntaxOn
\tl_new:N \__robExt_tmp_contain_code_tl
\NewDocumentCommand{\robExtCacheMeCode}{O{}+v}{%
{% Group
%% We store the input in a non-string element for efficiently implementing "auto forward"
\tl_set:Nn \__robExt_tmp_contain_code_tl {#2}
% fails with error Runaway argument?
\tl_replace_all:Nen \__robExt_tmp_contain_code_tl {\char_generate:nn{13}{12}} {\par}
\tl_show:N \__robExt_tmp_contain_code_tl
\begin{flushleft}\ttfamily%
\__robExt_tmp_contain_code_tl
\end{flushleft}%
}
}
\ExplSyntaxOff
\noindent This works:\robExtCacheMeCode{42 % 8}.
\noindent This fails, first because newlines are turned into caret caret M, but also becase characters like
:\robExtCacheMeCode{
Hey I am a multi-line text with { } # foo []<> % many caracters, including
newlines like her.
}.
\end{document}