TransWikia.com

Creating dynamic list variable with repeat command

TeX - LaTeX Asked on November 1, 2021

I need to create a list in LaTeX with n people and their respective sex:

adduser[M]{Pedro Jose}
adduser[M]{John Scar}
adduser[F]{Leila Brew}
...
adduser[F]{Larissa Yik}

When I compile it using LaTeX, I want it to produce something like that:

Pedro Jose is Male
John Scar is Male
Leila Brew is Female
...
Larissa Yik is Female

I tried primitive LaTeX repetition functions with a variable separated by ;, but it does not work. Any idea?


@UPDATE

Steven solved the question.

documentclass{minimal}
usepackage[utf8]{inputenc}

makeatletter
newcommand*userlist{}
newcommand*adduser[2][]
  {g@addto@macrouserlist{
    #2ifx M#1 is malefiifx F#1 is femalefi.\
  }}
makeatother

% User's to add
adduser[M]{Pedro Jose}
adduser[M]{John Scar}
adduser[F]{Leila Brew}
adduser[F]{Larissa Yik}

begin{document}

{noindenttextbf{User list:}}%

noindentuserlist 

end{document}

One Answer

I suggest using the datatool-package and maintaining two databases:

  • One database is used for adding entries.
    That database is to be saved to an external text-file at the end of the LaTeX-run.
    In the example below that database is called "usersinterim".
  • Another database is used for iterating on entries.
    That database is created at the beginning of the LaTeX-run by loading the external-text-file created during the previous run.
    That database contains all data-entries that were added in the previous LaTeX-run.
    In the example below that database is called "users".

This way you can create lists of all users etc even before having a user added.

But you need to compile the document at least twice.

documentclass{article}
usepackage{datatool}
usepackage{atveryend}

newcommandDATABASEFILECreateIfNotExistent[1]{%
  DTLifdbexists{#1}{}{%
    IfFileExists{jobname.#1}{%
      DTLloaddb{#1}{jobname.#1}%
    }{%
      DTLnewdb{#1}%
    }%
  }%
  DTLifdbexists{#1interim}{}{%
    DTLnewdb{#1interim}%
    AtEndDocument{%
      % This will save the interim-database to file with
      % the last shipout. The last shipout is triggered
      % by end{document}/enddocument.
      DTLsavedb{#1interim}{jobname.#1}%
    }%
    AfterLastShipout{%
      % Check whether the content of the database-token-register
      % stemming from the database-file of the previous LaTeX-run
      % differs from content of the database-token-register
      % stemming from the database-file of the current LaTeX-run.
      % If so, something changed and you need to re-run LaTeX.
      % Probably this should be omitted with large databases.
      begingroup
      DTLgdeletedb{#1interim}%
      DTLloaddb{#1interim}{jobname.#1}%
      edeftempa{thecsname dtldb@#1endcsname}%
      edeftempb{thecsname dtldb@#1interimendcsname}%
      ifxtempatempb
        endgroup
      else
        endgroup
        csname @latex@warning@no@lineendcsname{%
           Database `#1' may have changed.
           Rerun to get listings of entries etc right%
         }%
      fi
    }%
  }%
}%
newcommandusercount{0}%
newcommandstepusercount{%
  xdefusercount{numbernumexprusercount+1relax}%
}%
newcommandadduser[2][]{%
  stepusercount
  DATABASEFILECreateIfNotExistent{users}%
  DTLnewrow{usersinterim}%
  {%
    dtlexpandnewvalue
    DTLnewdbentry{usersinterim}{primary key}{usercount}%
  }%
  DTLnewdbentry{usersinterim}{name}{#2}%
  DTLnewdbentry{usersinterim}{gender}{#1}%
}%
newcommandGenderListOfAllUsers{%
  DATABASEFILECreateIfNotExistent{users}%
  DTLforeach{users}{NameField=name, GenderField=gender}{%
      parnoindent
      NameField
      expandafterlowercaseexpandafter{expandafterifxGenderField}m%
        space is male%
      else
        expandafterlowercaseexpandafter{expandafterifxGenderField}f%
          space is female%
        else
          's gender is not identified%
        fi
      fi
      .%
  }%
}%

begin{document}

noindenttextbf{List of all users:}medskip

GenderListOfAllUsers

bigskip

noindenttextbf{Some text.}

bigskip

adduser[M]{Pedro Jose}
adduser[M]{John Scar}
adduser[F]{Leila Brew}
adduser[F]{Larissa Yik}
adduser[?]{John Doe}
adduser{Smith, John}

noindenttextbf{List of all users:}medskip

GenderListOfAllUsers

end{document}

enter image description here

Answered by Ulrich Diez on November 1, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP