56

I would like to have some variables for showing or hiding text in Latex.

For example, I would like to have two versions of a document. A short version and a long version by changing a variable at the top of the latex document.

For example:

I want to set a variable long - true

and in the text I would like to use if long == true show text (long version) else if false do not show the text (short version).

Any examples?

2
  • 3
    You might like the tagging package, wich was created by one of our users (Brent Longborough), also have a look at this question. Jun 5, 2012 at 11:12
  • 2
    use the package comment. It allows to handle different parts of the document. You'll get the documentation with texdoc comment
    – user2478
    Jun 5, 2012 at 11:23

3 Answers 3

63
\documentclass{article} 
\usepackage{lipsum}    
\newif\iflong

\begin{document}

  \longtrue   
  \iflong \lipsum[1] \else short version \fi 

  \longfalse
  \iflong \lipsum[1] \else short version \fi 
\end{document}   

With ifthen

\documentclass{article} 
\usepackage{ifthen}
\newboolean{long}   

\begin{document}

\setboolean{long}{false}   
\ifthenelse{\boolean{long}}{long version}{short version} 

\setboolean{long}{true}
\ifthenelse{\boolean{long}}{long version}{short version}  
\end{document} 
1
18

I second Herbert on using the comment package. :) Here's a possible solution with etoolbox and \iftoggle{<name>}{<true>}{<false>}:

\documentclass{article}

\usepackage{etoolbox}

\providetoggle{long}
\settoggle{long}{true}

\begin{document}

\iftoggle{long}{Overhead the albatross hangs motionless upon the air.}%
{And deep beneath the rolling waves in labyrinths of coral caves.}

\end{document}

There's also \nottoggle{<name>}{<not true>}{<not false>} which negates the test.

6

A solution which does not require loading a package is

\documentclass{article}

\newif\iflong
\longtrue % switch to false in short version
\newcommand{\inLongVersion}[1]{\iflong #1\fi}

\begin{document}

Overhead the albatross hangs motionless upon the air. %
\inLongVersion{And deep beneath the rolling waves in labyrinths of coral caves.}

\end{document}

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .