3

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?

Mel
  • 5,837
  • 10
  • 37
  • 42
Chang
  • 396
  • 4
  • 17
  • What is the expected output given your LaTeX input? That is, what does `\input[option=c]{class}` (say) translate to? – Werner Oct 15 '17 at 07:07
  • I have updated. I want to output different part of a single tex file. – Chang Oct 15 '17 at 18:25

3 Answers3

2

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

tom
  • 1,303
  • 10
  • 17
0

As I understand it, what you want is to update the function for each different part of the text, while defining the function in just one place.

The easy way to do this is to renew a variable command at the start of each section.

At start:

\newcommand{\VARIABLENAME}{VARIABLE_1}

At section:

\renewcommand{\VARIABLENAME}{VARIABLE_2}

There are more advanced ways of doing this as well, involving defining variables but for all it is worth, this is more readable and simpler to implement.

Note: If you are planning to make something more dynamic then just a class, I recommend implementing something in another language such as python to write the file in LaTex as it usually gives a lot more room for modification.

Mel
  • 5,837
  • 10
  • 37
  • 42
pippidis
  • 23
  • 4
0

You can use the following (crude) method of identifying the textual components between which you want to extract stuff from a file:

enter image description here

\documentclass{article}

\usepackage{filecontents}
\begin{filecontents*}{class.tex}
   switch(option)
     case a
       some text a
     case b
       some text b
     case c
       some text c
     case d
       some text d
   endswitch
\end{filecontents*}

\usepackage{catchfile}

% \inputclass{<file>}{<from>}{<to>}
\newcommand{\inputclass}[2]{%
  \CatchFileDef{\class}{class.tex}{}%
  \long\def\classsegment##1#1 ##2 #2##3\relax{##2}%
  \show\classsegment
  \expandafter\classsegment\class\relax
}

\begin{document}

\inputclass{case c}{case d}

\inputclass{case a}{case b}

\inputclass{case d}{endswitch}

\inputclass{case b}{case c}

\end{document}

Related:

The last one is a more adaptable approach using the catchfilebetweentags package. This requires the insertion of appropriate tags within your code, which might not be as helpful. You could also use listings to include specific lines of code from an external file.

Werner
  • 14,324
  • 7
  • 55
  • 77