GeneralProblem comparing values within ifthen

LaTeX specific issues not fitting into one of the other forums of this category.
Post Reply
pvnotp
Posts: 4
Joined: Fri Apr 17, 2009 2:16 am

Problem comparing values within ifthen

Post by pvnotp »

Hello all,
I'm trying to create a series of macros that will automatically generate a quiz for a student based upon their past performance on similar quizzes. I've defined a command \grade{student name}{concept number} that gives out a student's grade on the given concept without problem. For instance \grade{Taybra}{3} prints as "2". I then defined a command to give the corresponding quiz thusly:

Code: Select all

\newcommand{\autoquiz}[2]{
	\ifthenelse{\grade{#1}{#2} < 4}{easy quiz}{hard quiz}\
}
However, when I put it into action with

Code: Select all

\autoquiz{Taybra}{3}
I get the cryptic error message: "! Argument of \@tempc has an extra }."

I assume this has something to do with the internals of the ifthen package, but I don't know how to fix it.

Here's my preamble, if it makes a difference.

Code: Select all

\documentclass[12pt]{article}
\usepackage{fullpage}
\usepackage{ifthen}
\pagestyle{empty}
\begin{document}
I'd appreciate any advice on how to fix this problem, even if the advice is that this is a totally impractical way to approach my goal and I should be doing something else entirely.

Thanks!

Recommended reading 2024:

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

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

josephwright
Site Moderator
Posts: 814
Joined: Tue Jul 01, 2008 2:19 pm

Re: Problem comparing values within ifthen

Post by josephwright »

Please post a full example, including your definition of \grade. There are lots of things we don't know here, so giving a useful answer is a bit hard.
Joseph Wright
pvnotp
Posts: 4
Joined: Fri Apr 17, 2009 2:16 am

Problem comparing values within ifthen

Post by pvnotp »

Sorry, I'm new to the community. I hope this gives enough information.

Code: Select all

\documentclass[12pt]{article}
\usepackage{fullpage}
\usepackage{ifthen}
\pagestyle{empty}
\begin{document}

\newcommand{\grade}[2]{
	\ifthenelse{\equal{#1}{Taybra}}{
		\ifthenelse{#2 = 1}{3}{
        ...more ifthenelse clauses for more topics...}
   }{...more ifthenelse clauses for more students...}
}

\newcommand{\autoquiz}[2]{
	\ifthenelse{\grade{#1}{#2} < 4}{easy quiz}{hard quiz}
}

\autoquiz{Taybra}{1}
\end{document}
I just tried this now and received the error:

! Argument of \@tempc has an extra }.
<inserted text>
\par
l.18 \autoquiz{Taybra}{1}

Although very strangely, the error sometimes appears as "! Argument of \equal has extra }." instead.

Thanks so much for your help.
phi
Posts: 577
Joined: Tue Oct 21, 2008 8:10 pm

Problem comparing values within ifthen

Post by phi »

The \ifthenelse tests and thus \grade are unexpandable. Try to get rid of the tests and to use a different structure. The following might serve as a starting point.

Code: Select all

\documentclass{article}
\usepackage{etoolbox}
\newcommand*{\setgrade}[3]{%
    \csdef{grade-#1-#2}{#3}%
}
\newcommand*{\grade}[2]{%
    \csuse{grade-#1-#2}%
}
\newcommand*{\autoquiz}[2]{
    \ifnum\grade{#1}{#2}<4 %
    easy quiz%
    \else
    hard quiz%
    \fi
}
\setgrade{Taybra}{1}{3}

\begin{document}
\autoquiz{Taybra}{1}
\end{document}
pvnotp
Posts: 4
Joined: Fri Apr 17, 2009 2:16 am

Re: Problem comparing values within ifthen

Post by pvnotp »

Wonderful! Not only does it work, this is a much more economical solution for \grade. I will have to explore this etoolbox package fully. Thank you so much!
Post Reply