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.

Following up on a previous question about universal processing of some hundreds of exercises to prepare for my students has arisen a new problem that I try to resolve ... however the fact is that I do not know if LaTeX can handle what I try to do nor know how to do it ...

Εach exercise is a separate small file, and refers to the entire spectrum of physics. I have input some flags within these separate files, such as energy, entropy, solid body, etc. so that I can sort these.

Well my question. Is there a way through these flags that can be used to printed only the input files that refer to a flag that I will choose in the main document without printing all other files? Νamely if I want only exercises containing flag entropy Is there a way to not print the exercise.436 that does not contain flag entropy ... say something like \chose{entropy}

\documentclass[12pt]{book}
\usepackage{xcolor}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% This piece of code its from previous question
%% And is due to @egreg

\newif\ifflags
\long\def\flags[#1]{%
  \ifflags
  \par
  \begingroup
  \let \\ \par
   \color{red}\small\\ \textbf{Flags: #1}%
  \par\medskip
  \endgroup
  \fi}

\flagstrue  % comment out for don't appears the flags
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

\begin{document}

\input{exercise.435.tex}
\input{exercise.436.tex}

\end{document

The files exercise.435 at the beginning contains the following lines

\flags[energy, entropy, solid.boby]
here is the text of exercise.435.tex

The files exercise.436 at the beginning contains the following lines

\flags[energy, solid.boby]
here is the text of exercise.436.tex

enter image description here

share|improve this question
add comment

2 Answers

up vote 4 down vote accepted

Here is a way with LaTeX3 macros

\documentclass{article}
\usepackage{xparse}

\ExplSyntaxOn
\NewDocumentCommand{\enableflags}{ m }
 {
  \karathan_enable_flags:n { #1 }
 }

\NewDocumentCommand{\flags}{ O{} }
 {
  \karathan_flags:n { #1 }
 }

\seq_new:N \g_karathan_enabled_flags_seq
\bool_new:N \l_karathan_flag_found_bool

\cs_new_protected:Npn \karathan_enable_flags:n #1
 {
  \seq_gset_from_clist:Nn \g_karathan_enabled_flags_seq { #1 }
 }

\cs_new_protected:Npn \karathan_flags:n #1
 {
  \bool_set_false:N \l_karathan_flag_found_bool
  \clist_map_inline:nn { #1 }
   {
    \seq_if_in:NnT \g_karathan_enabled_flags_seq { ##1 } 
     { \clist_map_break:n { \bool_set_true:N \l_karathan_flag_found_bool } }
   }
  \bool_if:NF \l_karathan_flag_found_bool { \endinput }
 }
\ExplSyntaxOff

\begin{document}

\section{Enabled \texttt{energy}}

\enableflags{energy}

\input{exercise.435.tex}

\input{exercise.436.tex}

\section{Enabled \texttt{entropy}}

\enableflags{entropy}

\input{exercise.435.tex}

\input{exercise.436.tex}

\section{Enabled \texttt{solid.body}}

\enableflags{solid.body}

\input{exercise.435.tex}

\input{exercise.436.tex}

\section{Enabled \texttt{energy}, \texttt{entropy}, \texttt{solid.body}}

\enableflags{energy, entropy, solid.body}

\input{exercise.435.tex}

\input{exercise.436.tex}

\end{document}

The \enableflags command receives as argument the list of flags you want to enable. Then \flags compares each item in its argument with this list. If no match is found, the file is closed and no more input from it is accepted (with \endinput).

enter image description here


If you want an and and not an or of the flags, here's how you can define the macro \karathan_flags:n:

\cs_new_protected:Npn \karathan_flags:n #1
 {
  \bool_set_true:N \l_karathan_flag_found_bool
  \seq_map_inline:Nn \g_karathan_enabled_flags_seq
   {
    \clist_if_in:nnF { #1 } { ##1 }
     { \seq_map_break:n { \bool_set_false:N \l_karathan_flag_found_bool } }
   }
  \bool_if:NF \l_karathan_flag_found_bool { \endinput }
 }

If all set flags are in the argument of \flags, then the boolean remains true and \endinput is not executed.

Here's a complete example:

\documentclass{article}
\usepackage{xparse,indentfirst}

\ExplSyntaxOn
\NewDocumentCommand{\enableflags}{ m }
 {
  \karathan_enable_flags:n { #1 }
 }
\NewDocumentCommand{\resetflags}{ m }
 {
  \seq_gclear:N \g_karathan_enabled_flags_seq
  \karathan_enable_flags:n { #1 }
 }

\NewDocumentCommand{\flags}{ O{} }
 {
  \karathan_flags:n { #1 }
 }

\seq_new:N \g_karathan_enabled_flags_seq
\bool_new:N \l_karathan_flag_found_bool

\cs_new_protected:Npn \karathan_enable_flags:n #1
 {
  \seq_gset_from_clist:Nn \g_karathan_enabled_flags_seq { #1 }
 }

\cs_new_protected:Npn \karathan_flags:n #1
 {
  \bool_set_true:N \l_karathan_flag_found_bool
  \seq_map_inline:Nn \g_karathan_enabled_flags_seq
   {
    \clist_if_in:nnF { #1 } { ##1 }
     { \seq_map_break:n { \bool_set_false:N \l_karathan_flag_found_bool } }
   }
  \bool_if:NF \l_karathan_flag_found_bool
   %{  \texttt{(#1)}\par }
   { \endinput }
 }

\ExplSyntaxOff

\begin{document}

\section{Enabled \texttt{energy}}

\enableflags{energy}

\input{exercise.435.tex}

\input{exercise.436.tex}

\section{Enabled \texttt{entropy}}

\enableflags{entropy}

\input{exercise.435.tex}

\input{exercise.436.tex}

\section{Enabled \texttt{energy}, \texttt{entropy}}

\enableflags{energy,entropy}

\input{exercise.435.tex}

\input{exercise.436.tex}

\section{Enabled \texttt{solid.body}}

\enableflags{solid.body}

\input{exercise.435.tex}

\input{exercise.436.tex}

\section{Enabled \texttt{energy}, \texttt{entropy}, \texttt{solid.body}}

\enableflags{energy, entropy, solid.body}

\input{exercise.435.tex}

\input{exercise.436.tex}

\end{document}

enter image description here

share|improve this answer
    
For not to violate the rules I will not say "thank you" but I will say that some of us owe so much to some of you who have real passion with TeX-LaTex etc :) –  karathan Nov 25 '12 at 23:25
    
I asked exactly the same and @DavidCarlisle (copy-paste) :-) Ηow I could change your code so that is "true" if simultaneously met two or more flags? as an example if I choose flags energy and entropy then file exercise.436 isn't displayed that having only flag energy ? –  karathan Nov 26 '12 at 22:46
    
@karathan I added the change you requested –  egreg Nov 26 '12 at 23:08
    
I have replaced the piece of macro, and while displaying the flags does not display the content of exercises, perhaps, something I do not understand correctly. I will testing again and will inform you of the result –  karathan Nov 27 '12 at 0:02
    
@karathan These macros don't display the flags. –  egreg Nov 27 '12 at 0:07
show 5 more comments

This version just use primitive TeX conditionals, you can specify any number of flags in separate \chooseflag commands, the file will be printed if it contains any chosen flag.

enter image description here

\documentclass[12pt]{book}
\usepackage{xcolor}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% This piece of code its from previous question
%% And is due to @egreg

\newif\ifflags
\long\def\flags[#1]{%
  {\xflags#1,xstopx,\endinput%
  \ifx\endinput\relax
  \ifflags
  \par
  \begingroup
  \let \\ \par
   \color{red}\small\\ \textbf{Flags: #1}%
  \par\medskip
  \endgroup
  \fi\fi}}

\def\xflags#1,#2{%
\csname xflags#1\endcsname
\xflags#2}

\def\xflagsxstopx#1{}

\def\chooseflag#1{\expandafter\def\csname xflags#1\endcsname{\let\endinput\relax}}

\chooseflag{entropy}

\flagstrue  % comment out for don't appears the flags
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

\begin{document}

\input{exercise.435.tex}
\input{exercise.436.tex}

\end{document}
share|improve this answer
    
Is it possible to add more than one flag in the code? –  karathan Nov 25 '12 at 20:13
    
Sorry my english not help me ... you write it at the beginning :) –  karathan Nov 25 '12 at 20:31
    
Ηow I could change your code so that is "true" if simultaneously met two or more flags? as an example if I choose flags energy and entropy then file exercise.436 isn't displayed that having only flag energy? –  karathan Nov 26 '12 at 6:36
    
oh you want to and the flags together not or ?? OK I misunderstood the requirement yes I'll update the code in a bit once I have a bit of time during day –  David Carlisle Nov 26 '12 at 9:37
add comment

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.