尝试在宏中使用“xstring”命令时出现错误

尝试在宏中使用“xstring”命令时出现错误

当我尝试编译下面的最小示例时,出现错误:

! Argument of \@xs@StrLeft has an extra }.
<inserted text> 
                \par 
l.33 \setInSCCF{abcdef}

如果我在命令中\setInSC切换用法,那么它就会起作用。所以一定有与命令相关的东西。但是,单独使用时可以正常工作。我首先尝试扩展参数,但无济于事(但不知道我是否做对了)。\textbf\setInSCCF\setInSC\setInSC

\documentclass[a4paper, 11pt]{article}

\usepackage{textcase}
\usepackage{xstring}

\makeatletter
\DeclareRobustCommand{\setInSC}[1]{%
  \ifx\f@shape\code@Roman
    \expandafter\textsc{\MakeTextLowercase{#1}}%
  \else
    \ifdim\fontdimen\@ne\font>\z@
      \begingroup
      \check@mathfonts
      \fontsize\sf@size\z@\selectfont
      \let\MakeTextLowercase\@firstofone
      \expandafter\MakeUppercase{#1}%
      \endgroup
    \else
      \expandafter\MakeTextUppercase{#1}%
    \fi
  \fi
}
\newcommand*{\code@Roman}{n}
\newcommand*{\code@Italics}{it}
\makeatother

\DeclareRobustCommand{\setInSCCF}[1]{%
  \StrLeft{#1}{1}\setInSC{\StrGobbleLeft{#1}{1}{}}%
}


\begin{document}
\setInSCCF{abcdef}
\end{document}

答案1

您不能将StrGobbleLeft其用作命令的参数。您需要一个临时命令来保存结果。

\DeclareRobustCommand{\setInSCCF}[1]{%
  \StrLeft{#1}{1}%
  \StrGobbleLeft{#1}{1}[\tempa]%
  \setInSC{\tempa}%
}

完整示例:

\documentclass[a4paper, 11pt]{article}

\usepackage{textcase}
\usepackage{xstring}

\makeatletter
\DeclareRobustCommand{\setInSC}[1]{%
  \ifx\f@shape\code@Roman
    \textsc{\MakeTextLowercase{#1}}%
  \else
    \ifdim\fontdimen\@ne\font>\z@
      \begingroup
      \check@mathfonts
      \fontsize\sf@size\z@\selectfont
      \let\MakeTextLowercase\@firstofone
      \MakeUppercase{#1}%
      \endgroup
    \else
      \MakeTextUppercase{#1}%
    \fi
  \fi
}
\newcommand*{\code@Roman}{n}
\newcommand*{\code@Italics}{it}
\makeatother

\DeclareRobustCommand{\setInSCCF}[1]{%
  \StrLeft{#1}{1}%
  \StrGobbleLeft{#1}{1}[\tempa]%
  \setInSC{\tempa}%
}


\begin{document}
\setInSCCF{abcdef}
\end{document}

要拆分字符串,您可以xstring使用以下代码来避免:

\documentclass[a4paper, 11pt]{article}
\makeatletter
\def\setIn#1{\@setIn#1\@nil}
\def\@setIn#1#2\@nil{%
Input One: #1\par
Input two: #2%
}
\makeatother
\begin{document}
\setIn{abcdef}
\end{document}

相关内容