GeneralName Seperator (IF conditon for \def)

LaTeX specific issues not fitting into one of the other forums of this category.
Post Reply
SathishV
Posts: 2
Joined: Fri May 31, 2013 9:04 am

Name Seperator (IF conditon for \def)

Post by SathishV »

Hi Forum,

I am new to Latex, just a Beginner, Basically a perl developer,

I have created a latex file

Code: Select all

\begin{document}

\cont{\iname{\isurname{Name1}\ifname{K. C. G.}}\iname{\isurname{Name11}\ifname{Y. Q.}}\iname{\isurname{Name111}\ifname{C.-Z.}}\ititles{Some title Text}\ipage{995}}

\end{document}
Now my need is to define the latex commands, and the display should come as the below

Name1 K. C. G., Name11 Y. Q. and Name111 C.-Z. Some title Text - 995.

\iname - Person Name will repeat several times
\isurname - Surname of a person
\ifname - First Name of a person

\iname will repeat any number of times, say 1 to ...

If only one \iname found inside \cont, display should be like the below
Name1 K. C. G. Some title Text - 995.

If only two \iname found inside \cont, display should be like the below
Name1 K. C. G. and Name11 Y. Q. Some title Text - 995.

If more than two \iname found inside \cont, display should be like the below (and should come after last \iname)
Name1 K. C. G., Name11 Y. Q. and Name111 C.-Z. Some title Text - 995.


I can able to define for \iname seperated by comma, but i don't know how to write a macro to define \iname with different outputs based on the if condition,

Is it possible to define that, i am totally a beginner for latex, please guide me

Thanks & Regards
Sathish V.
Last edited by cgnieder on Wed Sep 18, 2013 11:04 am, edited 1 time in total.

Recommended reading 2024:

LaTeXguide.org • LaTeX-Cookbook.net • TikZ.org
LaTeX Beginner's Guide LaTeX Cookbook LaTeX TikZ graphics TikZによるLaTeXグラフィックス
User avatar
cgnieder
Site Moderator
Posts: 2000
Joined: Sat Apr 16, 2011 7:27 pm

Name Seperator (IF conditon for \def)

Post by cgnieder »

I have a feeling you're trying to reinvent the wheel, namely a bibliography. There already are many powerful solutions for this. Nevertheless, for the fun of it here is one possibility. I tried to add comments that hopefully explain what the code does:

Code: Select all

\documentclass{article}
% useful LaTeX programming tools:
\usepackage{etoolbox}
% make @ behave like a letter:
\makeatletter
\newbool{incont}
\newcommand*\cont[1]{%
  \begingroup
    \booltrue{incont}%
    #1%
  \endgroup
}
% an internal list:
\def\@iname@list{}
% raise an error if \iname is used outside \cont:
\newcommand*\iname[1]{%
  \ifbool{incont}
    {\@iname{#1}}
    {%
      \@latex@error
        {\string\iname\space used outside of \string\cont!}
        {%
          You cannot use \string\iname\space outside of \string\cont!
          Check your code.%
        }
    }%
}
% add name to internal list and print list of no further name follows:
\def\@iname#1{%
  \listadd\@iname@list{#1}%
  \@ifnextchar\iname
    {}
    {\@iname@print}%
}
% counters for comparison:
\newcounter{@iname@total}
\newcounter{@iname@step}
% print the list:
\def\@iname@print{%
  \setcounter{@iname@total}{0}%
  \setcounter{@iname@step}{0}%
  % determine the total number of names:
  \forlistloop{\stepcounter{@iname@total}\@gobble}\@iname@list
  % its just one name? Print it. Else add the appropriate separators:
  \ifnumequal{\value{@iname@total}}{1}
    {\forlistloop{\@firstofone}\@iname@list}
    {\forlistloop{\@iname@separator}\@iname@list}%
  % clear list:
  \def\@iname@list{}%
}
\def\@iname@separator#1{%
  \stepcounter{@iname@step}%
  % a total of two names:
  \ifnumequal{\value{@iname@total}}{2}
    {%
      % add ` and ' between names:
      \ifnumequal{\value{@iname@step}}{\value{@iname@total}}
        {#1}
        {#1 and }%
    }
    {%
      % three or more names: add a `, ' between names and `, and ' between the
      % last two:
      \ifnumequal{\value{@iname@step}}{\value{@iname@total}-1}
        {#1, and }
        {%
          \ifnumequal{\value{@iname@step}}{\value{@iname@total}}
            {#1}
            {#1, }%
        }%
    }%
}

% make @ an other character again:
\makeatother

\begin{document}

\cont{\iname{First Name}}

\cont{\iname{First Name}\iname{Second Name}}

\cont{\iname{First Name}\iname{Second Name}\iname{Third Name}}

\cont{\iname{First Name}\iname{Second Name}\iname{Third Name}\iname{Fourth Name}}

\end{document}
Regards
site moderator & package author
User avatar
cgnieder
Site Moderator
Posts: 2000
Joined: Sat Apr 16, 2011 7:27 pm

Name Seperator (IF conditon for \def)

Post by cgnieder »

Please always give a link if you cross-post such as this one on TeX.sx. Our board rules clearly state this!
site moderator & package author
Post Reply