Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

Is there a way to turn certain parts of the text on and off in LyX/Latex ?

I'm thinking of having something like an environment (lets call it "TL" for "traffic light") and a parameter t such that if I set for example t=0 (meaning the light shows "red") at the beginning, all pieces of my text that lie within the TL-environment aren't displayed in the compiled document - whereas if I set t=1 ("green") they are displayed ?

(I asked about something like such an environment following the idea that LyX maybe has special mechanism to do this - so any solution, not necessarily "hard-coded" ones via environments are welcome as long as they are easy to implement.)

share|improve this question

2 Answers 2

up vote 4 down vote accepted

Lyx solution:

Lyx have can insert branches, that are chunks of text that can be activated or deactivated.

In Document → Settings → Branches, or with the Insert menu you can define several branches, select one in the list of Available Branches and then insert a chunk of text as usually in Lyx.

For example, in exam with multiple choice questions, after each question you can insert a chunk of the branch "Short answers" with the correct choice, as well as another chunk of the branch "Long answer" with the correct choice and some explanation of why this is the right answer. Then you can obtain a PDF of that exam with only the question, another including the short answers only for a quick check of each exam, and even a third PDF including the long answers for the student feed-back.

share|improve this answer
    
+1: Branches really are the LyX way to approach this! –  Daniel Dec 19 '12 at 21:55

You can use the etoolbox to set up a boolean which is either true or false

\newbool{hidetrafficlight}
\setbool{hidetrafficlight}{false}

and then depending on what value it takes, use \AtBeginEnvironment from the etoolbox to comment out the environment using comment from the verbatim package

% set conditional behaviour of environment
\ifbool{hidetrafficlight}{\AtBeginEnvironment{trafficlight}{\comment}%
\AtEndEnvironment{trafficlight}{\endcomment}}{}

Complete MWE

\documentclass{article}
\usepackage{lipsum}
\usepackage{etoolbox}   % for booleans and much more
\usepackage{verbatim}   % for the comment environment

% setup a new boolean
\newbool{hidetrafficlight}
\setbool{hidetrafficlight}{false}

% new environment
\newenvironment{trafficlight}{}{}

% set conditional behaviour of environment
\ifbool{hidetrafficlight}{\AtBeginEnvironment{trafficlight}{\comment}%
\AtEndEnvironment{trafficlight}{\endcomment}}{}


\begin{document}

\subsection*{Traffic light environment}
\begin{trafficlight}
  \lipsum[1]
\end{trafficlight}

\subsection*{After traffic light}
\lipsum[2]

\end{document}
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.