宏的问题

宏的问题

我不明白为什么这个乳胶代码不能按预期工作:

\documentclass{article}

\usepackage{xstring}

\newcommand{\doparamG}[2]{%
\StrCut{#2}{"}\LeftString\RightString%
\doparam{#1}{\LeftString}%
\StrCut{\RightString}{"}\RLeftString\RRightString%
\StrLen{\RLeftString}[\rlstrLen]%
\StrLen{\RRightString}[\rrstrLen]%
\ifnum\rlstrLen > 0 M:#1{\RLeftString}\fi%
\ifnum\rrstrLen > 0 \doparamG{#1}{\RRightString}\fi%
}

\newcommand{\doparam}[2]{%
\StrCut{#2}{ }\LeftString\RightString%
\StrLen{\LeftString}[\lstrLen]%
\StrLen{\RightString}[\rstrLen]%
\ifnum\lstrLen > 0 #1{\LeftString}\fi%
\ifnum\rstrLen > 0 \doparam{#1}{\RightString}\fi%
}

\newcommand{\doparamGDEBUG}[2]{%
\StrCut{#2}{"}\LeftString\RightString%
Left:\LeftString\\%
\StrCut{\RightString}{"}\RLeftString\RRightString%
\StrLen{\RLeftString}[\rlstrLen]%
\StrLen{\RRightString}[\rrstrLen]%
Middle:\RLeftString ,len: \rlstrLen\\%\ifnum\rlstrLen > 0 #1{\RLeftString}\fi%
Right:\RRightString ,len: \rrstrLen\\%\ifnum\rrstrLen > 0 \doparamGDBG{#1}{\RRightString}\fi%
\ifnum\rrstrLen > 0 \doparamGDEBUG{#1}{\RRightString}\fi%
}

\newcommand{\justnewline}[1]{#1\\}

\begin{document}
Simple test:\\
\doparam{\justnewline}{ line1 line2   line3 }
\doparamG{\justnewline}{ "line 4" line5   "line 6" }

But we have:\\
\doparamGDEBUG{\justnewline}{ "line 4" line5   "line 6" }

\end{document}

答案1

替代方案expl3和一些黑客技术。

\documentclass{scrartcl}
\usepackage{etoolbox}
\usepackage{xparse}

\newcommand\justpar[1]{#1\par}

\ExplSyntaxOn

\NewDocumentCommand \doparam { O{\justpar} m }
 {
  \abdollah_doparam:Nn #1 { #2 }
 }
\cs_new_protected:Npn \abdollah_doparam:Nn #1 #2
 {
  \tl_set:Nn \l_tmpa_tl { #2 }
  \tl_replace_all:Nnn \l_tmpa_tl { " } { \abdollah_doparam_quote:w }
  \tl_set:Nx \l_tmpa_tl { \l_tmpa_tl }
  \seq_set_split:NnV \l_tmpa_seq { ~ } \l_tmpa_tl
  \seq_map_inline:Nn \l_tmpa_seq { \tl_if_empty:nF {##1} { #1 {##1} } } 
 }
\cs_new:Npn \abdollah_doparam_quote:w #1 \abdollah_doparam_quote:w { { #1 } }

\ExplSyntaxOff

\begin{document}

\doparam{ line1 line2   line3  "line 4" line5   "line 6" }

\end{document}

相关内容