寻求了解如何扩展子字符串,以便可以在 etoolbox 包中的 ifboolexpr 中评估值 - 作为 latex 的新手(仅使用过去 18 个月),我终于开始了解它如何组合在一起的基本原理。现在的问题是,我对这个系统越来越感兴趣,但没有编程背景,对一些概念感到困惑,是的,我在发布问题之前已经阅读了包手册,但无法理解如何将它们组合在一起的逻辑,所以任何帮助都会很棒。
我努力了:
- xstring 包-不可扩展,
- substr 包 - ? 没有运气
- etextools 包 - 应该可以扩展,但我无法扩展 - 尝试了 ExpandNext 和 expandnext,但没有成功
- xparse - 查看文档并记下来 - 甚至无法理解它
尝试了所有这些包来实现我的最终目标,但没有成功!
我可以从数据集中获取子字符串,但无法让 etoolbox 包评估结果,因此我无法使用它 - 结果会产生错误。我猜是因为子字符串不可扩展,但我不知道如何让它扩展,尽管我尝试这样做,即使在阅读包文档之后也是如此 :(
最终目标是让结果可供其他宏使用。基本上,我正在评估原始数据输入并确认数据是否符合我的标准要求,如果不符合 - 那么我希望能够使用修订后的数据作为输入到 Latex 的标准。现在,在任何人提供原始 TEX 答案之前,请知道我在理解 Latex 和使用各种软件包方面已经够挣扎的了,更不用说原始 TEX 编码了,所以如果可能的话,使用 LATEX 软件包的回复将不胜感激
这是我的 MWE,我展示了几个包来证明我可以提取所需的字符串 - 参见结果 2-4,但是,我在 etoolbox 包中的 ifboolexpr 中嵌套了结果 2,但它提供了“没有结果”如您所见,问题是如何扩展我的子字符串以便 etoolbox 包可以对其进行评估,或者还有其他方法吗?
如果我的解释很长请原谅,但这是我在这里的第一篇帖子。
\documentclass[11pt,a4paper]{book}
\usepackage[utf8]{inputenc}
\usepackage{etoolbox}
\usepackage{substr}
\usepackage{etextools}
%Custom commands
\newcommand{\nl}{\vspace{1.0\baselineskip} \\}
\newcommand{\str}{Step}
\newcommand{\rcata}{M} %INDI SEX TAG
\newcommand{\rcatb}{dma} %INDI_Relationship Class
\newcommand{\rcatc}{my fathers first wife is my Step mother but is not biologically related to me.} %INDI_Relationship Input string
%START
\begin{document}
\section*{Code Outputs}
First Result: \ifboolexpr{
test {\ifstrequal{\rcata}{M}}%Test INDI Sex
and
test {\ifstrequal{\rcatb}{dmd}}%Test Indi Relationship Class
or
test {\ifstrequal{\rcatb}{dma}}%Test Indi Relationship Class
}
{\DeclareStringFilter\FindSTEP{wife}%See if WIFE in long string
\FindSTEP?.{my fathers first wife is my Step mother but is not biologically related to me.}{imd}{\rcatb}}{\LARGE{This Test Failed}} \normalsize
\\ (trying to expand one test within other tests - but doesn't work) \nl
\paragraph{}
\Large{\textbf{Below are single expansions test only}} \\ \normalsize All are successful by themselves but will not work inside a nested expression - Need to use ``EXPAND' somehow but cannot figure how it has to be done though \nl
Second Result: \DeclareStringFilter\FindSTEP{Step}
\FindSTEP?.{my fathers first wife is my (Step) mother but is not biologically related to me.}{imd - This test was successful}{\rcatb}
\\
using the etextools package \nl
Third Result: \IfSubStringInString{Step}{my fathers first wife is my (Step) mother but is not biologically related to me.}{imd - This test was successful}{This Test Failed} \\ This test uses the substr package with verbatim text only \nl
Forth Result:
\IfSubStringInString{\str}{\rcatc}{imd - This test was successful}{This Test Failed} \\ This Test uses the substr package but calls a macro - was testing to see if it would do it
\end{document}
答案1
你etextools
确实应该避免。
主要问题是它\ifstrequal
不能扩展它的参数,但是你可以让它这样做。
\documentclass[11pt,a4paper]{book}
\usepackage[utf8]{inputenc}
\usepackage{etoolbox}
\usepackage{etextools}
%Custom commands
\newcommand{\str}{Step}
\newcommand{\rcata}{M} %INDI SEX TAG
\newcommand{\rcatb}{dma} %INDI_Relationship Class
\newcommand{\rcatc}{my fathers first wife is my Step mother but
is not biologically related to me.} %INDI_Relationship Input string
\begin{document}
\ifboolexpr{
test {\expandafter\ifstrequal\expandafter{\rcata}{M}}%Test INDI Sex
and
test {\expandafter\ifstrequal\expandafter{\rcatb}{dmd}}%Test Indi Relationship Class
or
test {\expandafter\ifstrequal\expandafter{\rcatb}{dma}}%Test Indi Relationship Class
}
{\DeclareStringFilter\FindSTEP{wife}%See if WIFE in long string
\FindSTEP?.{my fathers first wife is my Step mother but is not
biologically related to me.}{imd}{\rcatb}}{This Test Failed}
\end{document}
使用(无错误的)编程环境的不同实现expl3
:
\documentclass[11pt,a4paper]{book}
\usepackage{xparse}
\ExplSyntaxOn
% provide some user level commands
\cs_new_eq:NN \booleanTF \bool_if:nTF
\cs_new_eq:NN \stringequal \str_if_eq_p:ee
\prg_generate_conditional_variant:Nnn \str_if_in:nn { ee } { T,F,TF }
\cs_new_eq:NN \stringinTF \str_if_in:eeTF
\ExplSyntaxOff
%Custom commands
\newcommand{\str}{Step}
\newcommand{\rcata}{M} %INDI SEX TAG
\newcommand{\rcatb}{dma} %INDI_Relationship Class
\newcommand{\rcatc}{my fathers first wife is my Step mother but
is not biologically related to me.} %INDI_Relationship Input string
\begin{document}
\booleanTF{
\stringequal{\rcata}{M}%Test INDI Sex
&&
\stringequal{\rcatb}{dmd}%Test Indi Relationship Class
||
\stringequal{\rcatb}{dma}%Test Indi Relationship Class
}
{\stringinTF{\rcatc}{wife}{imd}{\rcatb}}{This Test Failed}
\end{document}
逻辑上唯一的变化是,要搜索的字符串\stringinTF
是第二个参数。
两种代码均打印
国际医学发展研究所