Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Switch Case Conditional output in Latex

I want to write a report which has a structure like this:

   \begin{document}
     \input[option=a]{class}
     \input[option=b]{class}
     \input[option=c]{class}
     \input[option=d]{class}
   \end{document}

class.tex has content like this:

   here are some shared content

   switch(option)
     case a
       some text a
     case b
       some text b
     case c
       some text c
     case d
       some text d
   endswitch

   Here maybe more shared content.

Is there any way to do this in Latex?

like image 323
Chang Avatar asked Mar 01 '26 16:03

Chang


1 Answers

A simplified way of doing this could be with logic statements using if else fi logic

at the top of the .tex file set up a switch with

\newif\ifswitch

The default value will be false. To set the value to be true use

\switchtrue

Then in the text of the document use

\ifswitch

   <<text to include if switch is true>>

\else

   <<text to include if switch is false>>

\fi % ends the if statement

So for your particular question you could have a set of switches

\newifConditionA
\newifConditionB
\newifConditionC
\newifConditionD

This is not as elegant as using a switch statement, but allows conditions where you want text from A and C at the same time for example.

Reference where this is discussed is here for two versions of a document with 'if else' logic statements

like image 134
tom Avatar answered Mar 04 '26 20:03

tom