GeneralShow text if...

LaTeX specific issues not fitting into one of the other forums of this category.
gundegutt
Posts: 5
Joined: Tue Mar 23, 2010 12:33 am

Show text if...

Post by gundegutt »

Hi,

I use LaTex to do my assignments at uni, and want to make a if-statement that will either activate or de-activate a paragraph. I have used \newtheorem to make a {problem} environment which shows the problem as given in the assignment, but want to be able to hide this when I print out the assignment.

My plan is to create a counter:

Code: Select all

\newcounter{ShowProblem} 
\addtocounter{ShowProblem}{1} %Show problems if value == 1
and then use an if-statement to either display or hide a text.

Pseudocode:

Code: Select all

if ShowProblem == 1
\begin{problem}
bla..bla..bla
\end{problem}
end if
I've read some tutorials on the if-then environment, but are unable to get it working. Any other suggestions?

Recommended reading 2024:

LaTeXguide.org • LaTeX-Cookbook.net • TikZ.org
LaTeX Beginner's Guide LaTeX Cookbook LaTeX TikZ graphics TikZによるLaTeXグラフィックス
User avatar
gmedina
Posts: 2313
Joined: Wed Jul 11, 2007 11:45 pm

Show text if...

Post by gmedina »

Hi,

try the features offered by the comment package.
1,1,2,3,5,8,13,21,34,55,89,144,233,...
magicmoose
Posts: 90
Joined: Fri Nov 06, 2009 7:29 am

Show text if...

Post by magicmoose »

Or with ifthen, depending how you set up you problem environment/command

Code: Select all

\newcommand{\showproblems}{1}% set to 0 to hide problems
and then

Code: Select all

\ifthenelse{\equal{\showproblems}{1}}{true}{false}
For example,

Code: Select all

\newcommand{\problem}[1]{\ifthenelse{\equal{\showproblems}{1}}{#1}{}}
(although this is untested...)
gundegutt
Posts: 5
Joined: Tue Mar 23, 2010 12:33 am

Re: Show text if...

Post by gundegutt »

Thanks,

But could you please make your example a bit bigger? I didn't quite understand how to implement it..

Cheers
magicmoose
Posts: 90
Joined: Fri Nov 06, 2009 7:29 am

Re: Show text if...

Post by magicmoose »

Can you show us your code so we can see how you are currently doing your problems and give you a solution that will work how you want it.
gundegutt
Posts: 5
Joined: Tue Mar 23, 2010 12:33 am

Show text if...

Post by gundegutt »

magicmoose wrote:Can you show us your code so we can see how you are currently doing your problems and give you a solution that will work how you want it.
That would be excellent!

Code: Select all

\newtheorem{problem}{Problem}

if (showProblem == true)
  then{
       \begin{problem}
          Description of the problem given in the assignment
       \end{problem}
       }
endif
The if-then part is pseudocode, but illustrates what I want to achieve.
User avatar
frabjous
Posts: 2064
Joined: Fri Mar 06, 2009 12:20 am

Show text if...

Post by frabjous »

Code: Select all

\documentclass{article}

\usepackage{ifthen}

\newtheorem{problem}{Problem}
\newcommand{\showproblems}{1}% set to 0 to hide problems

\begin{document}

\noindent Here is some text so that something is always produced.

\ifthenelse{\equal{\showproblems}{1}}{
   \begin{problem}
  Description of the problem given in the assignment
   \end{problem}
}%
{}% if not set to 1
\end{document}
Magicmoose was also pointing in the direction of including the conditional in the definition of a \problem command so that you would not have to include the conditional every time. Perhaps the above is enough to see how that might work.
User avatar
gmedina
Posts: 2313
Joined: Wed Jul 11, 2007 11:45 pm

Show text if...

Post by gmedina »

An example using my initial suggestion:

Code: Select all

\documentclass{book}
\usepackage{comment}

\newtheorem{problem}{Problem}

\includecomment{comment}

\begin{document}

\noindent Here is some text so that something is always produced.

\begin{comment}
\begin{problem}
  Description of the problem given in the assignment.
\end{problem}
\end{comment}

\end{document}
comment out the line \includecomment{comment} to hide the contents of the problem environment.
1,1,2,3,5,8,13,21,34,55,89,144,233,...
gundegutt
Posts: 5
Joined: Tue Mar 23, 2010 12:33 am

Re: Show text if... (Solved)

Post by gundegutt »

Thanks for all help, gmedias solution was rather easy to implement so I went for the comment package.

I might have use for the other suggestions as well, if I want to hide different sections for instance.

Thanks
User avatar
gmedina
Posts: 2313
Joined: Wed Jul 11, 2007 11:45 pm

Show text if...

Post by gmedina »

With the comment package you can define different kinds of comments and include/exclude them at will:

Code: Select all

\documentclass{book}
\usepackage{comment}

\newtheorem{problem}{Problem}
\newtheorem{solution}{Solution}

\includecomment{commentp}
\excludecomment{comments}

\begin{document}

\noindent Here is some text so that something is always produced.

\begin{commentp}
\begin{problem}
  Description of the problem given in the assignment.
\end{problem}
\end{commentp}

Some more text

\begin{comments}
\begin{solution}
  Description of the solution to problem given in the assignment.
\end{solution}
\end{comments}

\end{document}
1,1,2,3,5,8,13,21,34,55,89,144,233,...
Post Reply