3

I'm trying to check if a section number belongs to a specified list:

    \ifnum\value{section}=\mylist % check if section number in mylist --- WRONG
        \dosomething
    \fi

The conditional is obviously wrong because it doesn't make sense to compare a number to a list. My sense is that there must be a quick fix (although my TeX knowledge is very limited). Please help!


p.s. Here's a more fully-spelled out version of the my question:

I'm trying to create an environment that can print selected sections only. I hope to specify in the beginning of the document a comma separated list of sections to be printed, and then print only selected sections. For example,

    \keepsections{1,5,8}
    \mysection{section 1}{content}
    \mysection{section 2}{content}
    \mysection{section 3}{content}
    \mysection{section 4}{content}
    \mysection{section 5}{content}
    \mysection{section 6}{content}
    \mysection{section 7}{content}
    \mysection{section 8}{content}

Should give us:

output

This is what I've tried:

    \documentclass{article}

    \newcommand{\keepsections}[1]{ \newcommand{\mylist}{#1} }
    \usepackage{environ}% http://ctan.org/pkg/environ
    \NewEnviron{customsection}{ % new environment
        \stepcounter{section} % environ should use the section counter
        \ifcustomsec % if want to print only select sections from a list
            \ifnum\value{section}=\mylist\relax % check if section in list --- WRONG
                \addtocounter{section}{-1} % use correct counter and print
                \BODY
            \fi
        \else % if want to print all sections,
            \addtocounter{section}{-1} % use correct counter and print
            \BODY
        \fi%
    }
    \newcommand{\mys}[1]{\begin{customsection}#1\end{customsection}}
    \newcommand{\mysection}[2]{\mys{\section{#1}#2}} % to conform to usage above

    \newif\ifcustomsec % Boolean to print only selected sections
    \customsectrue % Bool=True: Tell TeX to print only from the custom list
    \keepsections{1,5,8} % Print only sections 1 and 3

    \begin{document}

    \mysection{section 1}{content}
    \mysection{section 2}{content}
    \mysection{section 3}{content}
    \mysection{section 4}{content}
    \mysection{section 5}{content}
    \mysection{section 6}{content}
    \mysection{section 7}{content}
    \mysection{section 8}{content}

    \end{document}

Although it fails because of the wrong logic pointed out above.

This question -- Create odd-numbered answers, or all answers -- has a very nice solution to printing just even or odd sections that works really well, and that's where I took the idea from.

This question -- Test if a number is in a comma separated range/list of numbers -- discusses defining a command of the sort

    \ifinrange{<num>}{<range>}{<TRUE>}{<FALSE>}% num>=0

It would be nice if I could just replace

    \ifnum\value{section}=\mylist\relax%

portion with

    \ifinrange{\value{section}}{\mylist}{...}{...}

but with my limited knowledge I failed to implement it in this context.

Deeply appreciate any help. Thanks so much.

2

Using xinttools to loop over the comma separated list of section numbers:

\documentclass{article}
\usepackage{xinttools}

\newcommand{\keepsections}[1]{\newcommand{\mylist}{#1}}

\usepackage{environ}% http://ctan.org/pkg/environ
\NewEnviron{customsection}{% new environment
    \stepcounter{section}% environ should use the section counter
    \ifcustomsec % if want to print only select sections from a list
        \xintFor ##1 in \mylist \do
        {\ifnum\value{section}=##1\relax % check if section in list
            \addtocounter{section}{-1}% use correct counter and print
            \BODY
            \expandafter\xintBreakFor % no need to check farther in the list
        \fi}%
    \else % if want to print all sections,
        \addtocounter{section}{-1}% use correct counter and print
        \BODY
    \fi%
}
\newcommand{\mys}[1]{\begin{customsection}#1\end{customsection}}
\newcommand{\mysection}[2]{\mys{\section{#1}#2}}% to conform to usage above

\newif\ifcustomsec % Boolean to print only selected sections
\customsectrue % Bool=True: Tell TeX to print only from the custom list
\keepsections{1,5,8}% Print only sections 1, 5 and 8

\begin{document}
\mysection{section 1}{content}
\mysection{section 2}{content}
\mysection{section 3}{content}
\mysection{section 4}{content}
\mysection{section 5}{content}
\mysection{section 6}{content}
\mysection{section 7}{content}
\mysection{section 8}{content}
\end{document}

selected sections

  • I would vote up except being a new user I don't seem to have that privilege...but thanks a lot! – user174118 Feb 13 '14 at 10:53
  • you did yourself almost all of the work... so I upvoted your question ;-) – user4686 Feb 13 '14 at 17:53
  • I also just realized how recent a package xint is (02-05-14)! Given its newness I definitely could not have found it by myself. Thanks again for the cutting edge pointer and for answering the question so quickly :) – user174118 Feb 13 '14 at 18:46

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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