Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

I am new to LaTeX, just a beginner, basically a Perl developer.

I have created a LaTeX file

\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 LaTeX commands so that the output should appear as 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 define input for \iname separated by commas, but I don't know how to write a macro to define \iname with different outputs based on the if condition.

I hope to have clearly explained my doubt.

share|improve this question
    
Welcome to TeX.SX! You can have a look at our starter guide to familiarize yourself further with our format. –  Heiko Oberdiek Sep 18 '13 at 7:01
    
Cross-posted on LaTeX community –  cgnieder Sep 18 '13 at 10:02

2 Answers 2

Your input is very specific, and so is this answer. It assumes an \isurname-\ifname structure inside \iname:

enter image description here

\documentclass{article}
\newcounter{namecntr}\newcounter{curname}
\providecommand{\iname}[1]{}
\providecommand{\isurname}[1]{}
\providecommand{\ifname}[1]{}
\providecommand{\ititles}[1]{}
\providecommand{\ipage}[1]{}
\newcommand{\cont}[1]{{%
  \setcounter{namecntr}{0}\setcounter{curname}{0}% Restart counters
  \renewcommand*{\iname}{\stepcounter{namecntr}}% Set namecntr
  #1% Count number of names
  \renewcommand*{\iname}{\stepcounter{curname}% Set names
    \ifnum\value{curname}>1
      \ifnum\value{curname}<\value{namecntr}
        \unskip, 
      \else
        \unskip, and
      \fi\fi}%
  \renewcommand*{\isurname}[1]{##1}% Print surname
  \renewcommand*{\ifname}[1]{~##1}% Print first name
  \renewcommand*{\ititles}[1]{\unskip\space##1}% Print title
  \renewcommand*{\ipage}[1]{~-~##1}% Print page(s)
  #1% Process "list"
}}
\begin{document}

% One author
\cont{\iname{\isurname{Name1}\ifname{K. C. G.}}\ititles{Some title Text}\ipage{995}}

% No authors
\cont{\ititles{Some title Text}\ipage{995}}

% Two authors
\cont{\iname{\isurname{Name1}\ifname{K. C. G.}}\iname{\isurname{Name11}\ifname{Y. Q.}}\ititles{Some title Text}\ipage{995}}

% Three authors
\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}}

% Three authors with no page
\cont{\iname{\isurname{Name1}\ifname{K. C. G.}}\iname{\isurname{Name11}\ifname{Y. Q.}}\iname{\isurname{Name111}\ifname{C.-Z.}}\ititles{Some title Text}}

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

% Four authors with no title
\cont{\iname{\isurname{Name1}\ifname{K. C. G.}}\iname{\isurname{Name11}\ifname{Y. Q.}}\iname{\isurname{Name111}\ifname{C.-Z.}}\iname{\isurname{Name111}\ifname{C.-Z.}}\ipage{995}}

\end{document}
share|improve this answer
    
Thank you so much for you help, both the codings working very fine for me, it saved me a lot of time writing coding in perl, once again Thanks a lot –  Sathish V Sep 18 '13 at 8:34
    
@SathishV: Consider accepting one of the solutions as the answer. See How do you accept an answer? –  Werner Sep 18 '13 at 14:36

Please check whether the following MWE suits your requirement:

\documentclass{article}

\begin{document}
\pagestyle{empty}

\makeatletter
\newcounter{name}
\def\auCon{\unskip,\space}
\def\auAndCon{\unskip\space and\space}
\def\auStop{\space}
\long\def\cont#1{\setcounter{name}{0}%
  \expandafter\let\csname iname@ititles\endcsname\@undefined
  \expandafter\let\csname iname@ipage\endcsname\@undefined
  \def\isurname##1{\expandafter\gdef\csname iname@isurname@\thename\endcsname{##1}}%
  \def\ifname##1{\expandafter\gdef\csname iname@ifname@\thename\endcsname{##1}}%
  \def\iname##1{\stepcounter{name}##1}%
  \def\ititles##1{\expandafter\gdef\csname iname@ititles\endcsname{##1}}%
  \def\ipage##1{\expandafter\gdef\csname iname@ipage\endcsname{##1}}%
  #1%
  \@tempcnta=0%
  \@tempcntb=\thename%
  \advance\@tempcntb by -1%
  \loop\ifnum\@tempcnta<\thename%
    \advance\@tempcnta by 1%
    \xdef\myname{\the\@tempcnta}%
    \csname iname@isurname@\myname\endcsname\space%
    \csname iname@ifname@\myname\endcsname%
    \ifnum\@tempcnta<\@tempcntb%
      \auCon\else\ifnum\@tempcnta=\@tempcntb%
      \auAndCon\else\auStop\fi\fi%
  \repeat%
  \expandafter\ifx\csname iname@ititles\endcsname\relax\else%
  \textit{\csname iname@ititles\endcsname}\space\fi%
  \expandafter\ifx\csname iname@ipage\endcsname\relax\else%
  \textbf{\csname iname@ipage\endcsname}\fi\par
}
\makeatother

% One author
\cont{\iname{\isurname{Name1}\ifname{K. C. G.}}\ititles{Some title Text}\ipage{995}}

% No authors
\cont{\ititles{Some title Text}\ipage{995}}

% Two authors
\cont{\iname{\isurname{Name1}\ifname{K. C. G.}}\iname{\isurname{Name11}\ifname{Y. Q.}}\ititles{Some title Text}\ipage{995}}

% Three authors
\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}}

% Three authors with no page
\cont{\iname{\isurname{Name1}\ifname{K. C. G.}}\iname{\isurname{Name11}\ifname{Y. Q.}}\iname{\isurname{Name111}\ifname{C.-Z.}}\ititles{Some title Text}}

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

% Four authors with no title
\cont{\iname{\isurname{Name1}\ifname{K. C. G.}}\iname{\isurname{Name11}\ifname{Y. Q.}}\iname{\isurname{Name111}\ifname{C.-Z.}}\iname{\isurname{Name111}\ifname{C.-Z.}}\ipage{995}}

\end{document}
share|improve this answer
    
Thank you so much for you help, both the codings working very fine for me, it saved me a lot of time writing coding in perl, once again Thanks a lot, Tamila Boss neenga, me too Tamil –  Sathish V Sep 18 '13 at 8:33
    
@SathishV I am a Malayali, knows konjam konjam Tamil :-) –  Jagath AR Sep 18 '13 at 14:37

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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