Text FormattingConditional class?

Information and discussion about LaTeX's general text formatting features (e.g. bold, italic, enumerations, ...)
Post Reply
bluefoxicy
Posts: 5
Joined: Thu Jan 28, 2010 2:51 pm

Conditional class?

Post by bluefoxicy »

I'm trying to figure out how to write a formatting macro (I know very little latex but... it's a start) that does conditional compilation.

Let's say I have a math book with practice problems.. I'd want something like:

Code: Select all

3 \times 2 = \answersheet{6}
Then by setting a compile time switch (or a variable at the top of the file), I'd get "3 x 2 = 6" or "3 x 2 = ?" and perhaps have the actual answer value in red or something.

Does that make sense?

Recommended reading 2024:

LaTeXguide.org • LaTeX-Cookbook.net • TikZ.org

NEW: TikZ book now 40% off at Amazon.com for a short time.

And: Currently, Packt sells ebooks for $4.99 each if you buy 5 of their over 1000 ebooks. If you choose only a single one, $9.99. How about combining 3 LaTeX books with Python, gnuplot, mathplotlib, Matlab, ChatGPT or other AI books? Epub and PDF. Bundle (3 books, add more for higher discount): https://packt.link/MDH5p

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

Conditional class?

Post by magicmoose »

If you just want to make the answers appear or else replace them with an '??' then maybe something this simple would work for you.

Code: Select all

\newcommand{\answers}{yes}
\newcommand{\answer}[1]{\ifthenelse{\equal{\answers}{yes}}{#1}{??}}
Then wherever you want an answer just use

Code: Select all

\answer{insert answer here}
and set \answers to either yes for the answer to be displayed or anything else for the question marks.

See the following example:

Code: Select all

\documentclass{article}
\usepackage{ifthen}
\newcommand{\answers}{yes}
\newcommand{\answer}[1]{\ifthenelse{\equal{\answers}{yes}}{#1}{??}}
\begin{document}
3 \times 2 = \answer{6}
\renewcommand{\answers}{no}
3 \times 2 = \answer{6}
\end{document}
hope that helps
bluefoxicy
Posts: 5
Joined: Thu Jan 28, 2010 2:51 pm

Re: Conditional class?

Post by bluefoxicy »

Excellent. Thank you!
Post Reply