LaTeX:在字符串中使用一些字符

发布于 2024-09-27 06:18:46 字数 973 浏览 4 评论 0原文

我需要一个从字符串中提取数字对的宏,如下所示:

  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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

书间行客 2024-10-04 06:18:46

这似乎让您的测试正常工作:

\documentclass{article}

\def\macroGetPairs#1{\getPairs#1,.\wholeString}
\def\getPairs#1#2-#3#4,#5\wholeString {%
  Got pair (#1#2,#3#4).\\
\if#5.\else%
  \getPairs#5\wholeString
\fi}

\begin{document}
\noindent\macroGetPairs{10-43,40-51,60-73,83-97}
\end{document}

您的代码基本上可以正常工作,但是 \getPairs 无法匹配其最终扩展的输入(\getPairs 83-97)。您的递归结束测试 (\if#1.) 也在测试 #1 而不是 #5,这就是我的测试已经在这里完成了。也许如果有某种不同的方式将参数格式化为 \getPairs 就可以了。

This seems to get your test to work:

\documentclass{article}

\def\macroGetPairs#1{\getPairs#1,.\wholeString}
\def\getPairs#1#2-#3#4,#5\wholeString {%
  Got pair (#1#2,#3#4).\\
\if#5.\else%
  \getPairs#5\wholeString
\fi}

\begin{document}
\noindent\macroGetPairs{10-43,40-51,60-73,83-97}
\end{document}

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文