LaTeX:在字符串中使用一些字符
我需要一个从字符串中提取数字对的宏,如下所示:
n1-m1,n2-m2,n3-m3,n4-m4 (it could be longer)
其中 n1,m1,n2,m2,... 是 0 - 15 之间的数字。我怎样才能获取数字对 (n1,m1),以及我的宏中的 (n2,m2)、(n3,m3) 等?我需要使用每对一次,之后如果需要,我可以忽略该对。
假设每个数字都是 2 位数字(这不是一件优雅的事情),并且屠宰了 Debilski 在这个论坛中找到的代码,我设法让第一对执行以下操作:
\documentclass[11pt]{article}
\def\macroGetPairs #1{\getPairs#1.\wholeString}
\def\getPairs#1#2-#3#4,#5\wholeString {
\if#1.%
\else
% Test if pair was successfully extracted
Got pair (#1#2,#3#4). Still left: #5\\
% Begin recursion
%\takeTheRest#5\ofTheString
\fi}
\def\takeTheRest#1\ofTheString\fi
{\fi \getPairs#1\wholeString}
\begin{document}
\macroGetPairs{10-43,40-51,60-73,83-97}
\end{document}
但是,我不确定如何获得递归对我有用,可以得到其余的对。我认为只需取消注释该行
%\takeTheRest#5\ofTheString
就可以做到这一点,但它不起作用。请注意,宏的测试调用是:
\macroGetPairs{10-43,40-51,60-73,83-97}
有什么建议吗?非常感谢,
ERM
I need a macro that extracts pairs of number from a string that looks like this:
n1-m1,n2-m2,n3-m3,n4-m4 (it could be longer)
where n1,m1,n2,m2,... are numbers from 0 - 15. How can I go about getting the pairs (n1,m1), and (n2,m2), (n3,m3), etc inside my macro? I will need to use each pair once, after which I can, if needed, disregard the pair.
Assuming each digit is a 2-digit number (not an elegant thing to do), and butchering a code I found by Debilski in this forum, I managed to get the first pair doing the following:
\documentclass[11pt]{article}
\def\macroGetPairs #1{\getPairs#1.\wholeString}
\def\getPairs#1#2-#3#4,#5\wholeString {
\if#1.%
\else
% Test if pair was successfully extracted
Got pair (#1#2,#3#4). Still left: #5\\
% Begin recursion
%\takeTheRest#5\ofTheString
\fi}
\def\takeTheRest#1\ofTheString\fi
{\fi \getPairs#1\wholeString}
\begin{document}
\macroGetPairs{10-43,40-51,60-73,83-97}
\end{document}
However, I am not sure how to get the recursion working for me to get the rest of the pairs. I thought that simply uncommenting the line
%\takeTheRest#5\ofTheString
should do it, but it does not work. Note that the macro's test call is:
\macroGetPairs{10-43,40-51,60-73,83-97}
Any suggestions? Thank you very much,
ERM
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这似乎让您的测试正常工作:
您的代码基本上可以正常工作,但是
\getPairs
无法匹配其最终扩展的输入(\getPairs 83-97)。您的递归结束测试 (
\if#1.
) 也在测试#1
而不是#5
,这就是我的测试已经在这里完成了。也许如果有某种不同的方式将参数格式化为\getPairs
就可以了。This seems to get your test to work:
Your code was basically working, but there was no way for
\getPairs
to match its input on the final expansion (\getPairs 83-97
). Your end-of-recursion test (\if#1.
) was also testing#1
rather than#5
, which is what I've done here. Maybe if there was some different way of formatting the argument to\getPairs
that would have worked.