GeneralParsing through arguments in a macro

LaTeX specific issues not fitting into one of the other forums of this category.
Post Reply
estirodri
Posts: 2
Joined: Mon Oct 18, 2010 11:21 pm

Parsing through arguments in a macro

Post by estirodri »

Hi Everyone:

I am new to this forum, and obviously a first time poster. I have used Latex for a while, but generally just for typesetting easily done documents. However, I am writing a macro (first time as well) where I need to send pairs of arguments. I know there is a limit of nine for the number of arguments and that the limit can be circumvented through other means. However, I would like to call the macro as follows

\macrocall{n1-m1,n2-m2,n3-m3.}

where my pairs are (n1,m1), (n2,m2), etc. There will most likely be more than simply three pairs.

I wrote the following code to begin:

Code: Select all

\documentclass[11pt]{article}
\usepackage{ifthen}

%%%%%%%%%%%%%%%%%%%%%% Begin macro definition %%%%%%%%%%%%%%%%%%%%%
\def\macrotesting#1{\getPairs#1}
\def\getPairs#1-#2,#3.{
    % These are a couple of if statements testing the pairs
    % Ultimately I will need to test 26 pairs
    % The pair should be in arguments #1 and #2
    % The rest of the string should be in argument #3
    \ifthenelse{\equal{#1}{0}}{
      \ifthenelse{\equal{#2}{1}}{
         % Here I will do something else with the pairs, but I am 
         % just testing to see if I get the right pair
         #1 = number 1 (if it got here it should be 0)\\
         #2 = number 2 (if it got here it should be 1)\\
         #3 = number 3\\
         }{
      \ifthenelse{\equal{#2}{4}}{
         #1 = number 1 (if it got here it should be 0)\\
         #2 = number 2 (if it got here it should be 4)\\
         #3 = number 3\\}}}{}
}
%%%%%%%%%%%%%%%%%%%%%% End macro definition %%%%%%%%%%%%%%%%%%%%%%%

\begin{document}
% test the macro with a sample pairs string 
\macrotesting{0-4,0-1,0-3,3-7,4-16.}
\end{document}
which obviously does not work beyond getting the first pair. I just do not know HOW to call the macro recursively so that it gets all the pairs. Any comments on how to achieve this?

Second, my ifthenelse{} statements are going to get out of control in a hurry. Is there a better way to do this?

Finally, and this just out of curiosity, is there a character limit to a macro argument?

Thank you for any help.

ERM

Recommended reading 2024:

LaTeXguide.org • LaTeX-Cookbook.net • TikZ.org

NEW: TikZ book now 40% off at Amazon.com for a short time.

estirodri
Posts: 2
Joined: Mon Oct 18, 2010 11:21 pm

Parsing through arguments in a macro

Post by estirodri »

Hi Everyone:

I modified the if statements so they make more sense and so they are more readable. Here is the code:

Code: Select all

\documentclass[11pt]{article}
\usepackage{ifthen}

%%%%%%%%%%%%%%%%%%%%%% Begin macro definition %%%%%%%%%%%%%%%%%%%%%
\def\macrotesting#1{\getPairs#1}
\def\getPairs#1-#2,#3.{
    % These are a couple of if statements testing the pairs
    % Ultimately I will need to test 26 pairs
    % The pair should be in arguments #1 and #2
    % The rest of the string should be in argument #3
    \ifthenelse{\(\equal{#1}{0} \and \equal{#2}{1}\)} {
         #1 = number 1 (if it got here it should be 0)\\
         #2 = number 2 (if it got here it should be 1)\\
         #3 = number 3\\
         }{}%
    \ifthenelse{\(\equal{#1}{2} \and \equal{#2}{0}\)} {   
         #1 = number 1 (if it got here it should be 0)\\
         #2 = number 2 (if it got here it should be 2)\\
         #3 = number 3\\
         }{}%
     % Bunch of if statements deleted for the rest of the pairs
}
%%%%%%%%%%%%%%%%%%%%%% End macro definition %%%%%%%%%%%%%%%%%%%%%%%

\begin{document}
\macrotesting{0-1,0-4,0-1,0-3,3-7.}
\end{document}
The questions posted in the original post remain:
1. How do I get more than ONE pair?
2. Is there a better way to do the if statements?

Thank you again,

ERM
Post Reply