LaTeX xstring \StrCut 带有宏

LaTeX xstring \StrCut 带有宏

我一直在尝试用非常复杂的代码\StrCutxstring包中提供一些格式化的输入,但却遇到了错误。

以下是 MWE:

\documentclass[a4paper]{article}
\usepackage{xstring}

\begin{document}

\newcommand{\test}{\emph{First} part:Second part}
\StrCut{\test}{:}\firstpart\secondpart%
\firstpart --- \secondpart%

\end{document}

我收到此错误:

! Use of \@xs@StrCut@@ doesn't match its definition.
\text@command #1->\def \reserved@a {
#1}\ifx \reserved@a \@empty \let \check@...
l.7 \StrCut{\test}{:}
\firstpart\secondpart%

有什么建议吗?

编辑 :

有了这个 MWE:

\documentclass[a4paper]{article}

\usepackage{xstring}

\begin{document}

\newcommand{\test}{\emph{First} part:Second part}
\newcommand{\testbis}{\test}

\expandarg\StrCut{\testbis}{:}\firstpart\secondpart%

\firstpart --- \secondpart%

\end{document}

我没有收到任何错误,但是输出不是我期望的,因为 \test 没有被切断。

答案1

您不需要\StrCut此应用程序。您的代码有两个问题:

  1. 你似乎想要在宏上方任意一层宏
  2. 您想使用诸如 之类的“危险”命令\emph,这些命令在完全扩展后无法存活。

您可以使用\protected@edef以下命令来解决这两个问题:

\documentclass{article}

\makeatletter
\newcommand{\splitatcolon}[3]{%
  \protected@edef\split@temp{#1}%
  \expandafter\split@colon\split@temp:\@nil{#2}{#3}%
}
\def\split@colon#1:#2\@nil#3#4{%
  \def#3{#1}%
  \if\relax\detokenize{#2}\relax
    % no colon
    \let#4\@empty
  \else
    \split@eatcolon#2\@nil{#4}%
  \fi
}
\def\split@eatcolon#1:\@nil#2{\def#2{#1}}
\makeatother

\begin{document}

\newcommand{\test}{\emph{First} part:Second part}
\newcommand{\testbis}{\test}

\splitatcolon{\emph{First} part:Second part}\firstpart\secondpart

\firstpart\ --- \secondpart

\splitatcolon{\test}\firstpart\secondpart

\firstpart\ --- \secondpart

\splitatcolon{\testbis}\firstpart\secondpart

\firstpart\ --- \secondpart

\splitatcolon{\emph{First} part:Second part:Third part}\firstpart\secondpart

\firstpart\ --- \secondpart

\splitatcolon{\secondpart}\secondpart\thirdpart

\firstpart\ --- \secondpart\ --- \thirdpart

\end{document}

在此处输入图片描述

相同的想法如下xstring

\documentclass{article}
\usepackage{xstring}

\makeatletter
\newcommand{\splitatcolon}[3]{%
  \protected@edef\split@temp{#1}%
  \saveexpandmode
  \expandarg\StrCut{\split@temp}{:}#2#3%
  \restoreexpandmode
}
\makeatother

\begin{document}

\newcommand{\test}{\emph{First} part:Second part}
\newcommand{\testbis}{\test}

\splitatcolon{\emph{First} part:Second part}\firstpart\secondpart

\firstpart\ --- \secondpart

\splitatcolon{\test}\firstpart\secondpart

\firstpart\ --- \secondpart

\splitatcolon{\testbis}\firstpart\secondpart

\firstpart\ --- \secondpart

\splitatcolon{\emph{First} part:Second part:Third part}\firstpart\secondpart

\firstpart\ --- \secondpart

\splitatcolon{\secondpart}\secondpart\thirdpart

\firstpart\ --- \secondpart\ --- \thirdpart

\end{document}

答案2

切割失败最有可能的原因是\emph,因此展开参数:

\documentclass[a4paper]{article}
\usepackage{xstring}

\begin{document}

\newcommand{\test}{\emph{First} part:Second part}
\expandarg\StrCut{\test}{:}{\firstpart}{\secondpart}%
\firstpart\ --- \secondpart%

\end{document}

更新

\documentclass[a4paper]{article}

%\usepackage{xstring}

\usepackage{xparse}

\ExplSyntaxOn

\NewDocumentCommand{\StrCutNew}{mmmm}{%
  \tl_set:Nx \l_tmpa_tl {#1}%
  \seq_set_split:NnV \l_tmpa_seq {#2} {\l_tmpa_tl}
  \expandafter\DeclareDocumentCommand\csname #3\endcsname{}{\seq_item:Nn \l_tmpa_seq{1}}
  \expandafter\DeclareDocumentCommand\csname #4\endcsname{}{\seq_item:Nn \l_tmpa_seq{2}}
}
\ExplSyntaxOff

\begin{document}

\newcommand{\test}{First part:Second part}
\newcommand{\testbis}{\test}
\StrCutNew{\test}{:}{firstpart}{secondpart}

\firstpart\ --- \secondpart

\StrCutNew{\test}{:}{firstpart}{secondpart}


\firstpart\ --- \secondpart

\end{document}

答案3

我发现了一些似乎对编辑很有效的东西:

\documentclass[a4paper]{article}

\usepackage{xstring}

\begin{document}

\newcommand{\test}{\emph{First} part:Second part}
\newcommand{\testbis}{\test}

\StrExpand[2]{\test}{\toto}
\StrExpand[2]{\testbis}{\totobis}

\expandarg\StrCut{\emph{First} part:Second part}{:}\firstpart\secondpart%
\firstpart --- \secondpart%

\expandarg\StrCut{\test}{:}\firstpart\secondpart%
\firstpart --- \secondpart%

\expandarg\StrCut{\testbis}{:}\firstpart\secondpart%
\firstpart --- \secondpart%

\expandarg\StrCut{\toto}{:}\firstpart\secondpart%
\firstpart --- \secondpart%

\expandarg\StrCut{\totobis}{:}\firstpart\secondpart%
\firstpart --- \secondpart%

\end{document}

它仍然无法解决我复杂代码中的问题,但我无法让 MWE 重现该问题。我会回来的 :D

感谢 Christian Hupfer 的帮助。

相关内容