beamer add tag
JeT
Following the split of navigation bar based on \lecture, I'd like now to combine different lectures to produce different `courses`.

I have three courses
1. `CourseOne` that uses only Lecture1.tex and Lecture2.tex
1. `CourseTwo` that uses only Lecture2.tex and Lecture3.tex
1. `CourseThree` that uses only Lecture3.tex

I choose to compile for `CourseOne` and define 
`\def\course{CourseOne}`

(Probably not the right approach but) I'd like to create 
`\Cinput` command based on 

1- a file that contains my lecture 
eg : `{Lecture2.tex}`

2- A flag that says to which course the lecture could belong to. eg : `{CourseOne,CourseTwo}`


Here is MWE
```

\documentclass{beamer}
\usetheme{Copenhagen}

%I define the course I want to display, based on a combination of lectures, each of them in an individual file
\def\course{CourseOne}

%CourseOne
%CourseTwo
%CourseThree

\setbeamercolor{lecture in toc}{parent=structure}

\usepackage{pgffor}
\usepackage{totcount}
\regtotcounter{part}

\begin{filecontents}[overwrite]{Lecture4.tex}
\lecture{Lecture1}{lec1}
\section{Section 1}
\subsection{Subsection 1a}
\frame{}
\subsection{Subsection 1b}
\frame{}
\subsection{Subsection 1c}
\frame{}

\section{Section 2}
\frame{}
\end{filecontents}

\begin{filecontents}[overwrite]{Lecture2.tex}
\lecture{Lecture2}{lec2}
\section{Section 3}
\subsection{Subsection 3a}
\frame{}
\subsection{Subsection 3b}
\frame{}
\subsection{Subsection 3c}
\frame{}
\end{filecontents}

\begin{filecontents}[overwrite]{Lecture3.tex}
\lecture{Lecture3}{lec3}
\section{Section 4}
\subsection{Subsection 4a}
\frame{}
\subsection{Subsection 4b}
\frame{}
\subsection{Subsection 4c}
\frame{}
\section{Section 5}
\subsection{Subsection 5a}
\frame{}
\subsection{Subsection 5b}
\frame{}
\subsection{Subsection 5c}
\frame{}
\end{filecontents}


\makeatletter
\AtBeginLecture{%
  \mode<beamer>{
    \part{\beamer@lecturename}
    \expandafter\NR@gettitle\expandafter{\beamer@lecturename}
    \label{part:\thelecture}
    \begin{frame}
        \begin{beamercolorbox}[sep=16pt,center]{part title}
          {\Large\textsc{Lecture}\par}
          {\large\insertpart\par}
        \end{beamercolorbox}
        \tableofcontents
    \end{frame}
  }
  \beamer@tocsectionnumber=0
}
\makeatother

%% I have a problem to conditionnally input the Lecture file
\newcommand{\Cinput}[2]{
\if\course =#2  % "one of the string in #2"
\input{#1}
\fi
}


\title{Some Title}

\begin{document}

\begin{frame}
	\tableofcontents
	\foreach\x in {1,...,\totvalue{part}}{%
		\vskip-0.4cm
		\protect\usebeamercolor[fg]{lecture in toc}%
		\vfill%
		\hyperlink{part:\x}{\nameref{part:\x}}%
		
		\tiny\tableofcontents[part=\x]%
	}%
\end{frame}

\Cinput{Lecture1.tex}{CourseOne}
\Cinput{Lecture2.tex}{CourseOne,CourseTwo}
\Cinput{Lecture3.tex}{CourseTwo,CourseThree}

%After compilation only Lecture1 and Lecture2 would be compiled for CourseOne
\end{document}
```
Top Answer
marmot
I would probably devise a slightly different strategy. I would define lecture collections via
```
\pgfkeys{/courses/.cd,one/.initial={1,2},
	two/.initial={2,3},
	three/.initial={3}}
```
This means that collection `one` contains lectures 1 and 2, `two` 2 and 3, etc. Then you can use a command `\CourseInput` that can be used to load a certain collection. E.g. `\CourseInput{one}` loads then collection `one`. Or, as the following example shows, if you use `\CourseInput{one,two}` then it will input the contents of `one` and `two`, but without inputting the common lecture 2 twice, and while ordering them. (If you want to input it twice, you can use `\CourseInput[union=false]{one,two}`.) 

```
\documentclass{beamer}
\usetheme{Copenhagen}
\setbeamercolor{lecture in toc}{parent=structure}

\usepackage{pgffor}
\usepackage{totcount}
\regtotcounter{part}

\begin{filecontents}[overwrite]{Lecture1.tex}
\lecture{Lecture1}{lec1}
\section{Section 1}
\subsection{Subsection 1a}
\frame{}
\subsection{Subsection 1b}
\frame{}
\subsection{Subsection 1c}
\frame{}

\section{Section 2}
\frame{}
\end{filecontents}

\begin{filecontents}[overwrite]{Lecture2.tex}
\lecture{Lecture2}{lec2}
\section{Section 3}
\subsection{Subsection 3a}
\frame{}
\subsection{Subsection 3b}
\frame{}
\subsection{Subsection 3c}
\frame{}
\end{filecontents}

\begin{filecontents}[overwrite]{Lecture3.tex}
\lecture{Lecture3}{lec3}
\section{Section 4}
\subsection{Subsection 4a}
\frame{}
\subsection{Subsection 4b}
\frame{}
\subsection{Subsection 4c}
\frame{}
\section{Section 5}
\subsection{Subsection 5a}
\frame{}
\subsection{Subsection 5b}
\frame{}
\subsection{Subsection 5c}
\frame{}
\end{filecontents}

\begin{filecontents}[overwrite]{Lecture4.tex}
\lecture{Lecture4}{lec4}
\section{Section 4}
\subsection{Subsection 4a}
\frame{}
\subsection{Subsection 4b}
\frame{}
\end{filecontents}

\begin{filecontents}[overwrite]{Lecture5.tex}
\lecture{Lecture5}{lec5}
\section{Section 5}
\subsection{Subsection 5a}
\frame{}
\subsection{Subsection 5b}
\frame{}
\end{filecontents}

\makeatletter
\AtBeginLecture{%
  \mode<beamer>{
    \part{\beamer@lecturename}
    \expandafter\NR@gettitle\expandafter{\beamer@lecturename}
    \label{part:\thelecture}
    \begin{frame}
        \begin{beamercolorbox}[sep=16pt,center]{part title}
          {\Large\textsc{Lecture}\par}
          {\large\insertpart\par}
        \end{beamercolorbox}
        \tableofcontents
    \end{frame}
  }
  \beamer@tocsectionnumber=0
}

\newif\ifpgf@course@union
\pgfkeys{/courses/.cd,union/.is if=pgf@course@union,union/.default=true}
\pgf@course@uniontrue

\newcommand{\CourseInput}[2][]{%
\pgfkeys{/courses/.cd,#1}%
\let\pgfutil@tmpc\relax
\pgfutil@for\pgfutil@tmpa:={#2}\do{%
\ifx\pgfutil@tmpc\relax
\edef\pgfutil@tmpc{\pgfkeysvalueof{/courses/\pgfutil@tmpa}}%
\else
\edef\pgfutil@tmpc{\pgfutil@tmpc,\pgfkeysvalueof{/courses/\pgfutil@tmpa}}%
\fi
}%
\ifpgf@course@union
\pgfmathtruncatemacro{\pgfutil@tmpd}{min(\pgfutil@tmpc)}%
\pgfmathtruncatemacro{\pgfutil@tmpe}{max(\pgfutil@tmpc)}%
\edef\pgfutil@tmpb{\pgfutil@tmpd}%
\c@pgf@counta\pgfutil@tmpd%
\pgfutil@loop
\advance\c@pgf@counta by1\relax 
\edef\pgfutil@tmpf{\noexpand\pgfutil@in@{,\the\c@pgf@counta,}{\pgfutil@tmpc}}%
\pgfutil@tmpf
\ifpgfutil@in@%
\edef\pgfutil@tmpb{\pgfutil@tmpb,\the\c@pgf@counta}%
\fi
\ifnum\c@pgf@counta<\pgfutil@tmpe
\pgfutil@repeat
\else
\edef\pgfutil@tmpb{\pgfutil@tmpc}%
\fi
\pgfutil@for\pgfutil@tmpa:={\pgfutil@tmpb}\do{%
\input{Lecture\pgfutil@tmpa.tex}}%
}%	
\makeatother


\title{Some Title}

\pgfkeys{/courses/.cd,one/.initial={1,2},
	two/.initial={2,5},
	three/.initial={3}}

\begin{document}

\begin{frame}
	\tableofcontents
	\foreach\x in {1,...,\totvalue{part}}{%
		\vskip-0.4cm
		\protect\usebeamercolor[fg]{lecture in toc}%
		\vfill%
		\hyperlink{part:\x}{\nameref{part:\x}}%
		
		\tiny\tableofcontents[part=\x]%
	}%
\end{frame}

\CourseInput{one,two}
%\CourseInput[union=false]{one,two}

\end{document}
```

The command is designed to be upgradable (sanity checks, etc.). (Update: I had forgotten that (La)TeX has a built-in membership test, `\in@`, in pgf it is called `\ifpgfutil@in@`. Big thanks go to Frougon for reminding me! :smile_cat:)
Answer #2
samcarter
You can use `\includeonlylecture{lec1,lec2}` to specify which lectures should be compiled:

```
\documentclass{beamer}
\usetheme{Copenhagen}

%CourseOne
\includeonlylecture{lec1,lec2}

%CourseTwo
%\includeonlylecture{lec2,lec3}

%CourseThree
%\includeonlylecture{lec3}

\setbeamercolor{lecture in toc}{parent=structure}

\usepackage{pgffor}
\usepackage{totcount}
\regtotcounter{part}

\begin{filecontents}[overwrite]{Lecture4.tex}
\lecture{Lecture1}{lec1}
\section{Section 1}
\subsection{Subsection 1a}
\frame{}
\subsection{Subsection 1b}
\frame{}
\subsection{Subsection 1c}
\frame{}

\section{Section 2}
\frame{}
\end{filecontents}

\begin{filecontents}[overwrite]{Lecture2.tex}
\lecture{Lecture2}{lec2}
\section{Section 3}
\subsection{Subsection 3a}
\frame{}
\subsection{Subsection 3b}
\frame{}
\subsection{Subsection 3c}
\frame{}
\end{filecontents}

\begin{filecontents}[overwrite]{Lecture3.tex}
\lecture{Lecture3}{lec3}
\section{Section 4}
\subsection{Subsection 4a}
\frame{}
\subsection{Subsection 4b}
\frame{}
\subsection{Subsection 4c}
\frame{}
\section{Section 5}
\subsection{Subsection 5a}
\frame{}
\subsection{Subsection 5b}
\frame{}
\subsection{Subsection 5c}
\frame{}
\end{filecontents}


\makeatletter
\AtBeginLecture{%
  \mode<beamer>{
    \part{\beamer@lecturename}
    \expandafter\NR@gettitle\expandafter{\beamer@lecturename}
    \label{part:\thelecture}
    \begin{frame}
        \begin{beamercolorbox}[sep=16pt,center]{part title}
          {\Large\textsc{Lecture}\par}
          {\large\insertpart\par}
        \end{beamercolorbox}
        \tableofcontents
    \end{frame}
  }
  \beamer@tocsectionnumber=0
}
\makeatother

%% I have a problem to conditionnally input the Lecture file
\newcommand{\Cinput}[2]{
\if\course =#2  % "one of the string in #2"
\input{#1}
\fi
}


\title{Some Title}

\begin{document}

\begin{frame}
	\tableofcontents
	\foreach\x in {1,...,\totvalue{part}}{%
		\vskip-0.4cm
		\protect\usebeamercolor[fg]{lecture in toc}%
		\vfill%
		\hyperlink{part:\x}{\nameref{part:\x}}%
		
		\tiny\tableofcontents[part=\x]%
	}%
\end{frame}

\input{Lecture4}
\input{Lecture2}
\input{Lecture3}

%After compilation only Lecture1 and Lecture2 would be compiled for CourseOne
\end{document}
```
Answer #3
frougon
This is based on [marmot's answer](https://topanswers.xyz/tex?q=1348#a1594) and should be functionally identical to it. Since I found all the `\pgfutil@whatever` thingies a bit hard on my weak eyes :-), I decided to reimplement marmot's `\CourseInput` macro using `expl3`.

Edit: marmot's initial code was a bit verbose, in part because he implemented comma list membership testing himself; he is now using `\pgfutil@in@` instead, which obviously makes the code shorter and the rewriting less compelling. Many thanks to him for always teaching us great Ti*k*Z and PGF tricks. :-)

In the case where `\ifpgf@course@union` is true (i.e., `union=false` not used in the optional argument of `\CourseInput`), I used a different algorithm. marmot's code does the following:

1. Compute the smallest lecture number *m* and the largest lecture number *M* from argument `#2`.

2. Iterate from *m* to *M* by increments of 1, adding the current value to the list `\pgfutil@tmpb` if, and only if it is a member of the list `\pgfutil@tmpc` obtained from the concatenation of lecture numbers from all courses contained in argument `#2`.

Since sequence membership tests and sequence sorting are built into `expl3`, my algorithm for this part is instead:

1. Build an unsorted list of the desired lecture numbers using the `\seq_if_in:NnF` membership test to prevent the addition of duplicates.

2. Sort this list using `\seq_sort:Nn`.

The end result is the same (though my algorithm should be faster with large, sparse lists of lecture numbers ;-)).

```
\documentclass{beamer}
\usepackage{xparse}
\usetheme{Copenhagen}
\setbeamercolor{lecture in toc}{parent=structure}

\usepackage{pgffor}
\usepackage{totcount}
\regtotcounter{part}

\begin{filecontents}[overwrite]{Lecture1.tex}
\lecture{Lecture1}{lec1}
\section{Section 1}
\subsection{Subsection 1a}
\frame{}
\subsection{Subsection 1b}
\frame{}
\subsection{Subsection 1c}
\frame{}

\section{Section 2}
\frame{}
\end{filecontents}

\begin{filecontents}[overwrite]{Lecture2.tex}
\lecture{Lecture2}{lec2}
\section{Section 3}
\subsection{Subsection 3a}
\frame{}
\subsection{Subsection 3b}
\frame{}
\subsection{Subsection 3c}
\frame{}
\end{filecontents}

\begin{filecontents}[overwrite]{Lecture3.tex}
\lecture{Lecture3}{lec3}
\section{Section 4}
\subsection{Subsection 4a}
\frame{}
\subsection{Subsection 4b}
\frame{}
\subsection{Subsection 4c}
\frame{}
\section{Section 5}
\subsection{Subsection 5a}
\frame{}
\subsection{Subsection 5b}
\frame{}
\subsection{Subsection 5c}
\frame{}
\end{filecontents}

\begin{filecontents}[overwrite]{Lecture4.tex}
\lecture{Lecture4}{lec4}
\section{Section 4}
\subsection{Subsection 4a}
\frame{}
\subsection{Subsection 4b}
\frame{}
\end{filecontents}

\begin{filecontents}[overwrite]{Lecture5.tex}
\lecture{Lecture5}{lec5}
\section{Section 5}
\subsection{Subsection 5a}
\frame{}
\subsection{Subsection 5b}
\frame{}
\end{filecontents}

\makeatletter
\AtBeginLecture{%
  \mode<beamer>{
    \part{\beamer@lecturename}
    \expandafter\NR@gettitle\expandafter{\beamer@lecturename}
    \label{part:\thelecture}
    \begin{frame}
        \begin{beamercolorbox}[sep=16pt,center]{part title}
          {\Large\textsc{Lecture}\par}
          {\large\insertpart\par}
        \end{beamercolorbox}
        \tableofcontents
    \end{frame}
  }
  \beamer@tocsectionnumber=0
}

\newif\ifpgf@course@union
\pgfkeys{/courses/.cd,union/.is if=pgf@course@union,union/.default=true}
\pgf@course@uniontrue

\ExplSyntaxOn
\seq_new:N \l__jet_lectures_seq
\cs_generate_variant:Nn \clist_map_inline:nn { x }

\NewDocumentCommand \CourseInput { O{} m }
  {
    \pgfkeys { /courses/.cd, #1}
    \seq_clear:N \l__jet_lectures_seq

    \ifpgf@course@union
      \clist_map_inline:nn {#2}
        {
          \clist_map_inline:xn { \pgfkeysvalueof { /courses/##1 } }
            {
              \seq_if_in:NnF \l__jet_lectures_seq {####1}
                { \seq_put_right:Nn \l__jet_lectures_seq {####1} }
            }
        }
      \seq_sort:Nn \l__jet_lectures_seq
        {
          \int_compare:nNnTF {##1} > {##2}
            { \sort_return_swapped: }
            { \sort_return_same: }
        }
    \else
      \clist_map_inline:nn {#2}
        {
          \clist_map_inline:xn { \pgfkeysvalueof { /courses/##1 } }
            { \seq_put_right:Nn \l__jet_lectures_seq {####1} }
        }
    \fi

    \seq_map_inline:Nn \l__jet_lectures_seq { \input { Lecture##1.tex } }
  }
\ExplSyntaxOff
\makeatother


\title{Some Title}

\pgfkeys{/courses/.cd,one/.initial={1,2},
	two/.initial={2,5},
	three/.initial={3}}

\begin{document}

\begin{frame}
	\tableofcontents
	\foreach\x in {1,...,\totvalue{part}}{%
		\vskip-0.4cm
		\protect\usebeamercolor[fg]{lecture in toc}%
		\vfill%
		\hyperlink{part:\x}{\nameref{part:\x}}%

		\tiny\tableofcontents[part=\x]%
	}%
\end{frame}

\CourseInput{one,two}
%\CourseInput[union=false]{one,two}

\end{document}
```

Enter question or answer id or url (and optionally further answer ids/urls from the same question) from

Separate each id/url with a space. No need to list your own answers; they will be imported automatically.