[Tex/LaTex] Enable and disable contents of \chapter command

conditionalsxkeyval

I have large set of contents for the user. For a different user I would like to show different contents. For this I would like to produce different output by showing and not showing some of the contents (say chapters sections subsection) of the document.

My Idea is: While writing chapter/section I declare it to enable showing to a specific user
by setting argument to true or false. For example,

if (user1 == true) then it should be there in user1.pdf

More specifically, something like:

Macro is: \chapter <chapterName1> [bool arg1][bool arg2][bool arg3]

\chapter <chapterName1> [user1==true][user2==false][user3==false] 

\chapter <chapterName2> [user1==true][user2==true][user3==false]

\section <sectionName1> [user1==true][user2==true][user3==true]

If I pass user1 and user3 as 'True', then my output should contain

command (ex) : \def\arg1=true, arg2=false, arg1=true \input{myfile}

chapterName1

sectionName1

How can I do this?

Best Answer

You can start from here. The methods below feature the environ package to skip content, but you could also try to use the comment package for this purpose.

\documentclass[openany]{scrbook}

\usepackage{environ}
\usepackage{pdftexcmds}

\newcommand{\username}{user1}

\makeatletter
\NewEnviron{condchapter}[2][user]{
    \ifnum\pdf@strcmp{#1}{\username}=\z@
        \chapter{#2}
        \BODY
    \else
    \fi
}
\makeatother

\begin{document}

\begin{condchapter}[user1]{Title-1}

Some text.

\end{condchapter}

\begin{condchapter}[user2]{Title-2}

Some text.

\end{condchapter}

\renewcommand{\username}{user2}

\begin{condchapter}[user2]{Title-3}

Some text.

\end{condchapter}

\end{document}

Another possibility with the list of enabled users:

\documentclass[openany]{scrbook}

\usepackage{environ}
\usepackage{xstring}

\newcommand{\username}{user1}

\makeatletter
\NewEnviron{condchapter}[2][user]{
    \IfSubStr{#1}{\username}{%
        \chapter{#2}
        \BODY
    }{}
    %\else
    %\fi
}
\makeatother

\begin{document}

\begin{condchapter}[user1]{Title-1}

Some text.

\end{condchapter}

\begin{condchapter}[user2]{Title-2}

Some text.

\end{condchapter}

\renewcommand{\username}{user2}

\begin{condchapter}[user2, user3]{Title-3}

Some text.

\end{condchapter}

\end{document}

PS: to create all files at once you can start from the answers here


Another possibility is to use only the comment package for this purpose without the modifications to the chapter command.

Related Question