2

I have a problem with how to reset the counter in a case where I am trying to print names in one row and the telephone numbers in another row. I know I could have avoided the problem, but it is required for the work I am doing.

Here is the result I have reached so far:

enter image description here

This is my code

\documentclass[a4paper,12pt]{article}

\newcounter{faglig}\renewcommand{\thefaglig}{\alph{faglig}}
\makeatletter
\let\contactrow\@gobble
\let\telephonerow\@gobble
%\setcounter{faglig}{0}
%\stepcounter{faglig}
%\textsuperscript{\,\thefaglig}

\def\contact#1{%
\ifx\contactrow\@gobble\def\contactrow{\textbf {Person:}}\fi
\expandafter\def\expandafter\contactrow\expandafter{\contactrow\hspace{1cm}#1\stepcounter{faglig}\textsuperscript{\,\thefaglig}}}

\setcounter{faglig}{0}
\def\telephone#1{%
\ifx\telephonerow\@gobble\def\telephonerow{\textbf {Telephone:}}\fi
\expandafter\def\expandafter\telephonerow\expandafter{\telephonerow\hspace{1cm}#1\stepcounter{faglig}\textsuperscript{\,\thefaglig}}}
\makeatother


\contact{John Doe}
\telephone{12345}
\contact{Jane Doe}
\telephone{91145}
\contact{Donald Trump}
\telephone{67890}

\begin{document}

\contactrow
\newline

\telephonerow

\end{document}

The problem is how to reset the counter to have the telephone numbers indexed accordingly to the names. The command that I thought would reset does not seem to work. As you can see I have tried some alternatives that are commented out. If anyone have other comments to the code I would appreciated that too. I am all new to this type of programming, but it is very fun. I hope anyone can direct me in the right way.

2

You need no counter, but expl3. ;-)

With \seq_indexed_map_function:NN we can use the index number of the items stored in a sequence.

I also added a macro to print the data in tabular form, just to make a different example.

\documentclass[a4paper,12pt]{article}
\usepackage{xparse}

\ExplSyntaxOn

\seq_new:N \g_osterlie_contact_person_seq
\seq_new:N \g_osterlie_contact_telephone_seq

% store the items in the appropriate sequence
\NewDocumentCommand{\contact}{m}
 {
  \seq_gput_right:Nn \g_osterlie_contact_person_seq { #1 }
 }
\NewDocumentCommand{\telephone}{m}
 {
  \seq_gput_right:Nn \g_osterlie_contact_telephone_seq { #1 }
 }

% Deliver the names, with the index (in alphabetical form) as a superscript
\NewDocumentCommand{\contactrow}{}
 {
  Person:
  \seq_indexed_map_function:NN \g_osterlie_contact_person_seq \osterlie_contact_print:nn
 }

% Deliver the phone number, with the index (in alphabetical form) as a superscript
\NewDocumentCommand{\telephonerow}{}
 {
  Telephone:
  \seq_indexed_map_function:NN \g_osterlie_contact_telephone_seq \osterlie_contact_print:nn
 }

% Deliver the data in tabular form
\NewDocumentCommand{\contacts}{}
 {
  \begin{tabular}
   {
    @{}
    l
    *{\seq_count:N \g_osterlie_contact_person_seq}{c}
    @{}
   }
  Person:
  \seq_map_function:NN \g_osterlie_contact_person_seq \osterlie_contact_tabular:n
  \\
  Telephone:
  \seq_map_function:NN \g_osterlie_contact_telephone_seq \osterlie_contact_tabular:n
  \end{tabular}
 }

\cs_new:Nn \osterlie_contact_print:nn
 {
  \hspace{1cm}#2\textsuperscript{\int_to_alph:n{#1}}
 }

\cs_new:Nn \osterlie_contact_tabular:n
 {
  & #1
 }

\ExplSyntaxOff

\contact{John Doe}
\telephone{12345}
\contact{Jane Doe}
\telephone{91145}
\contact{Donald Trump}
\telephone{67890}

\begin{document}

\contactrow\par
\telephonerow

\bigskip

\contacts

\end{document}

enter image description here

1

In your example, \setcounter{faglig}{0} is placed between the definitions of \contactrow and \telephonerow, this means it is executed before the document even starts and the counter is 0 anyway.

Place \setcounter{faglig}{0} instead at the start of the telephone row or between the lines where \contactrow and \telephonerow are used in the document:

\documentclass[a4paper,12pt]{article}

\newcounter{faglig}\renewcommand{\thefaglig}{\alph{faglig}}
\makeatletter
\let\contactrow\@gobble
\let\telephonerow\@gobble
%\setcounter{faglig}{0}
%\stepcounter{faglig}
%\textsuperscript{\,\thefaglig}

\def\contact#1{%
\ifx\contactrow\@gobble\def\contactrow{\textbf {Person:}}\fi
\expandafter\def\expandafter\contactrow\expandafter{\contactrow\hspace{1cm}#1\stepcounter{faglig}\textsuperscript{\,\thefaglig}}}


\def\telephone#1{%
\ifx\telephonerow\@gobble\def\telephonerow{\setcounter{faglig}{0}\textbf {Telephone:}}\fi
\expandafter\def\expandafter\telephonerow\expandafter{\telephonerow\hspace{1cm}#1\stepcounter{faglig}\textsuperscript{\,\thefaglig}}}
\makeatother


\contact{John Doe}
\telephone{12345}
\contact{Jane Doe}
\telephone{91145}
\contact{Donald Trump}
\telephone{67890}

\begin{document}

\contactrow
\newline

%\setcounter{faglig}{0}
\telephonerow

\end{document}

enter image description here

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.