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.
Edit
Improved version
There are two basically two problems:
\ifstrequal
does not expand its arguments
- The content of the environment variable has a trailing whitespace character
at the end, which means
yes
will become yes' '
, so that the test fails. I could not figure out the reason up to now.
By usage of the xstring
package command \StrGobbleRight
and \ifdefstring
from etoolbox
package, it is possible to cope around, hopefully, that it is just one character at the end to be deleted. See the effect as diagnostics in the top of the output document, when \temp
and \newtemp
macros output -- if the whitespace is removed, the content of the environment variable and the following text should be glued together.
I redefined the getenv
command to \newgetenv
and used an expanded \edef\temp
to make the \ifdefstring
command work with the literal yes
value.
\documentclass{book}%
\usepackage{etoolbox}%
\usepackage{xstring}%
\usepackage{catchfile}%
\newcommand{\getenv}[2][]{%
\CatchFileEdef{\temp}{"|kpsewhich --var-value #2"}{}%
\if\relax\detokenize{#1}\relax\temp\else\let#1\temp\fi}
%\getenv[\INCLUDE]{\string INCLUDE}
\def\newtemp{}%
\newcommand{\newgetenv}[2][]{%
\CatchFileEdef{\temp}{"|kpsewhich --var-value #2"}{}%
\StrGobbleRight{\temp}{1}[\newtemp]% Delete the trailing whitespace character
\if\relax\detokenize{#1}\relax\temp\else\edef#1{\newtemp}\fi%
}%
\begin{document}
\newgetenv[\INCLUDE]{INCLUDE}%
%%% Diagnostics:
\temp%
Some Text%
\newtemp%
Some Text%
First paragraph of first file.
Value of the INCLUDE envvar is as follows: \INCLUDE
\ifdefstring{\INCLUDE}{yes}{\input{file2}}{Nothing to do in here}
Last paragraph of first file.
\end{document}
The file file2.tex
just contains the line
\textbf{Hello World}
Another version (Thanks to the hint made by H. Oberdiek)
The trailing whitespace(\endlinechar
) can be omitted by using \endlinechar=-1\relax
as 3rd argument to \CatchFileEdef
command. This simplifies the handling of the environment variable macro and the usage of xstring
package can be dropped.
\documentclass{book}%
\usepackage{etoolbox}%
\usepackage{catchfile}%
\newcommand{\newgetenv}[2][]{%
\CatchFileEdef{\temp}{"|kpsewhich --var-value #2"}{\endlinechar=-1\relax}%
\if\relax\detokenize{#1}\relax\temp\else\edef#1{\temp}\fi%
}%
\begin{document}
\newgetenv[\INCLUDE]{INCLUDE}%
First paragraph of first file.
Value of the INCLUDE envvar is as follows: \INCLUDE
\ifdefstring{\INCLUDE}{yes}{\input{file2}}{Nothing to do in here}
Last paragraph of first file.
\end{document}
I did not update the screenshot, nothing essential has changed in the output.
Best Answer
With
ifthen