Text FormattingConditional Formatting using ifthen

Information and discussion about LaTeX's general text formatting features (e.g. bold, italic, enumerations, ...)
Post Reply
gibsonsdad
Posts: 4
Joined: Sat Oct 06, 2012 5:05 am

Conditional Formatting using ifthen

Post by gibsonsdad »

I am attempting to create a master document for creating multiple choice mathematics assessments. I want to be able to create two versions, the student version, and the teacher version. The student version should format all choices the same. The teacher version should indicate the correct answer choice by bold-facing the correct choice. I have seen this done, but it has been many years. I have attempted the following;

Code: Select all

\newcommand{\choice}{\textbf}
\newcommand{\distractor}{--??--}
\newcommand{\selector}{\ifthenelse{\equal{--??--}{T}}{\choice}{\distractor}}
Where I have inserted --??--, I cannot determine what to put. I want this to change by commenting out some signal line to produce one of the versions, while not commenting out the signal line produces the other.

I would be appreciative of any assistance.
Last edited by cgnieder on Fri Dec 27, 2013 1:26 pm, edited 1 time in total.

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

gibsonsdad
Posts: 4
Joined: Sat Oct 06, 2012 5:05 am

Conditional Formatting using ifthen

Post by gibsonsdad »

I solved it. It is really quite simple. I used the following code;

Code: Select all

\documentclass[12pt]{article}
    \usepackage{ifthen, color}
        \begin{document}
            %Create the boolean flag cs (Choice Selector)
            \newboolean{cs}
                %Set the value of cs
                \setboolean{cs}{false}
                %Create a new command to set the text color to red
                \newcommand{\setcstrue}{\textcolor{red}}
                %Create a new command to set the color as normal
                \newcommand{\setcsfalse}{}
                %Create the command to surround the correct selection
                \newcommand{\cs}{\ifthenelse{\boolean{cs}}{\setcstrue}             
                           {\setcsfalse}}
                    %Use the command 
                    \cs{Red}
        \end{document}
to set the correct choice to red if the boolean is set to true, and to print normally if the boolean is set to false.
Last edited by cgnieder on Fri Dec 27, 2013 1:27 pm, edited 1 time in total.
Post Reply