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.

Hi I would like to compare two comma separated lists.

\documentclass[a4paper]{article}    
\usepackage{comment}    
\usepackage{ifthen}
\usepackage{xargs}
\usepackage{xkeyval}
\usepackage{xfor}  


\begin{document}

\dosomething{\compilation} % compilation is comma separated text coming from the terminal (pdflatex "\def\compilation{cat,vat,rat,sat} \input{myfile.tex}") 


%arguments of the new command is another comma separated list I can Print this with #1
\newcommand{\chapterInput}[4][all]{%

%code to compare starts here
\newtoks\myValue
\myValue={#1}


begin{comment}
I wanted to do somthing like this
for(every elemnet in compilation)
{for(every element in myValue)
{\ifthenelse{\equal{elemnet in compilation}{every element in myValue}}
   {    
 \chapter{#3\label{#4}}
    \input{#2}
    }
   {\relax}
}}

end{comment}

%My approach is 

\makeatletter

\@for\@myarg:=\myValue\do
{

\@for\@myVar:=\compilation\do
{
   \ifthenelse
       {
         \equal{\@myarg}{\@myVar}
          {    
           \chapter{#3\label{#4}} \input{#2}
          }
          {\relax}
       }
}
}



\newcommand{\sectionInput}[4][all]{%
    \section{#3\label{#4}}
    \input{#2}
}
\newcommand{\subsectionInput}[4][all]{%

}

\chapterInput[rat,cat]{testChapter}{Test Chapter}{chap:testChapter}
\sectionInput[mat,sat]{testSection}{Test Section}{sect:testSection}
\subsectionInput[bat]{testSection}{A subsection on best}{subsect:testSubsection}
\subsectionInput[vat]{testSection}{This ia a subsection on grain}{subsect:testSubsection}


\end{document}

My approach is not working. What'a wrong with my approach? Any other solution would also be appreciated.

share|improve this question
    
Why not use the xstring package? It defines many useful macros for string manipulation. –  Jubobs Apr 10 '13 at 15:49
    
I'm trying to better understand what you are trying to accomplish. Are you looking for a command that takes two arguments and compares: 1) if both comma separated lists contain exactly the same components 2) if all the components of one list are contained in the other list 3) ? What does the command return? A true/false flag? –  Steven B. Segletes Apr 11 '13 at 18:05
    
Hi Thanks for the reply, I wanted to check,the two list has at least one same element, If so then it should execute "{ \chapter{#3\label{#4}} \input{#2} }else It should leave the loop. }" –  umaraman Apr 12 '13 at 9:40

1 Answer 1

Every time I am reading comma separated list I get the association l3clist. The suggestion below uses l3clist. I commented the code to see what happen.

The basic idea is the definition of the command \LevelInput which has the following syntax:

\LevelInput*{level}[comma list]{file name}{level title}{label}

The star and the comma list are optional. If you use the star the star version of the level is used. Based on this main function I defined \sectionInput. \chapterInput is commented because it's not available for article.:

\NewDocumentCommand \sectionInput { s }
 {
  \IfBooleanTF { #1 }
   {
    \LevelInput*{section}
   }
   {
    \LevelInput{section}
   }
 }

Your predefined comma list should be also provided in the main code. Therefor the command \ProvideDocumentCommand is suitable. It defines the command only if it's undefined.

After this small introduction here the mwe.

\RequirePackage{filecontents}
\begin{filecontents}{examplefile.tex}
START EXAMPLE FILE

\lipsum[1]    

END EXAMPLE FILE
\end{filecontents}
\documentclass{article}%
\usepackage{lipsum}%filling text
\usepackage{xparse}
\ExplSyntaxOn
% compilation is comma separated text coming %
% from the terminal (pdflatex "\def\compilation{cat,vat,rat,sat}
% \input{myfile.tex}") 
% providing command is it isn't defined.
\ProvideDocumentCommand \compilation {} { cat , vat , rat , sat }
\clist_new:N \l_umar_compilation_clist
\clist_set:No \l_umar_compilation_clist { \compilation }
\bool_new:N \l_umar_listelemtequal_bool
\bool_set_false:N \l_umar_listelemtequal_bool
\NewDocumentCommand \LevelInput { s m o  m m m }
 {
  %#1 star version
  %#2 level command -- chapter/section/section/...
  %#3 optinal argument
  %#4 file name
  %#5 level title
  %#6 label command
  \IfBooleanTF { #1 }
   {%has star
    \IfNoValueTF { #3 } % no optinal Argument
     {%no optinal Argument
      \umar_starinput:nnn { #2 } { #5 } { #4 }
     }
     {%optinal Argument
      \umar_selected_starinput:nnnn { #3 } { #2 } { #5 } { #4 }
     }   
   }
   {%has no star
    \IfNoValueTF { #3 } 
     {%no ptinal Argument
      \umar_input:nnnn { #2 } { #5 } { #6 } { #4 }
     }
     {%optinal Argument
      \umar_selected_input:nnnnn { #3 } { #2 } { #5 } { #6 } { #4 }
     }   
   }
 }

%\umar_starinput:nnn { level cmd } { level title } { file name } 
\cs_new:Npn \umar_starinput:nnn #1 #2 #3
 {
  \use:c { #1  }* { #2 } 
  \input { #3 }
 } 
%\umar_input:nnnn { level cmd } { level title } { label } { file name }  
\cs_new:Npn \umar_input:nnnn #1 #2 #3 #4
 {
  \use:c { #1  } { #2 } \label { #3 } 
  \input { #4 }
 } 
%\umar_selected_starinput:nnnn { list }{ level cmd } { level title } 
%         { file name } 
\cs_new:Npn  \umar_selected_starinput:nnnn #1 #2 #3 #4
 {
  \clist_map_inline:nn { #1 }
   {
    \clist_if_in:NnT \l_umar_compilation_clist { ##1 }
     { \bool_set_true:N \l_umar_listelemtequal_bool }
   }
   \bool_if:NTF \l_umar_listelemtequal_bool 
    {
     \umar_starinput:nnn { #2 } { #3 } { #4 }
    }
    {
     \msg_log:n { no~input }
    }
  \bool_set_false:N \l_umar_listelemtequal_bool    
 }
%\umar_selected_input:nnnnn { list }{ level cmd } { level title } 
%         { label } { file name } 
\cs_new:Npn  \umar_selected_input:nnnnn #1 #2 #3 #4 #5
 {
  \clist_map_inline:nn { #1 }
   {
    \clist_if_in:NnT \l_umar_compilation_clist  { ##1 }
     { \bool_set_true:N \l_umar_listelemtequal_bool }
   }
   \bool_if:NTF \l_umar_listelemtequal_bool 
    {
     \umar_input:nnnn { #2 } { #3 } { #4 } { #5 }
    }
    {
     \msg_log:n { no~input }
    }
  \bool_set_false:N \l_umar_listelemtequal_bool    
 }

% not for article
%\NewDocumentCommand \chapterInput { s }
% {
%  \IfBooleanTF { #1 }
%   {
%    \LevelInput*{chapter}
%   }
%   {
%    \LevelInput{chapter}
%   }
% }
\NewDocumentCommand \sectionInput { s }
 {
  \IfBooleanTF { #1 }
   {
    \LevelInput*{section}
   }
   {
    \LevelInput{section}
   }
 }
\NewDocumentCommand \subsectionInput { s }
 {
  \IfBooleanTF { #1 }
   {
    \LevelInput*{subsection}
   }
   {
    \LevelInput{subsection}
   }
 }
\ExplSyntaxOff

\begin{document}
\tableofcontents

\sectionInput{examplefile}{first section}{sec:one}

\sectionInput[cat]{examplefile}{first section with opt}{sec:two}


\subsectionInput[fish,rat]{examplefile}{first subsection with opt}{subsec:one}


\subsectionInput*{examplefile}{first subsection with star}{}

\end{document}
share|improve this answer

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.