5

Imagine the following situation: A school offers two courses, course A and course B. One of the courses is math and the other is physics. Math is taught by Bill, and physics is taught by Susan. I want to implement these associations using ifthen package, so here is what I attempted:

\documentclass{article}

\usepackage{ifthen}

\newcommand{\course}[1]{
\ifthenelse{\equal{#1}{A}}{math}{}%
\ifthenelse{\equal{#1}{B}}{physics}{}%
}

\newcommand{\teacher}[1]{
\ifthenelse{\equal{\course{#1}}{math}}{Bill}{}%
\ifthenelse{\equal{\course{#1}}{physics}}{Susan}{}%
}

\begin{document}

Course: \course{A}
Teacher: \teacher{A}

\end{document}

which gives a massive amount of errors. So if/then can't handle a new command? Is there any way to implement this?

ashpool
  • 1,361

1 Answers1

4

There are many ways to do this. Here is one using a single, primitive \if (since you only have two courses):

Course: math
Teacher: Bill

\documentclass{article}

\newcommand{\course}{%
  \ifmathcourse
    math%
  \else% not mathcourse
    physics%
  \fi
}

\newcommand{\teacher}{%
  \ifmathcourse
    Bill%
  \else% not mathcourse
    Susan%
  \fi
}

\newif\ifmathcourse% default is \mathcoursefalse
\setlength{\parindent}{0pt}% Just for this example

\begin{document}

\mathcoursetrue% This is a math course
Course: \course \par
Teacher: \teacher

\end{document}

Perhaps, for multiple course, you can define each in terms of their name and teacher by some id. Below, \setcourse{<course>}{<name>}{<teacher>} sets this up, while \coursename and \courseteacher extracts that content.

Course: Mathematics
Teacher: Bill
Course: Physics
Teacher: Susan

\documentclass{article}

\newcommand{\currentcourse}{}
\newcommand{\course}[1]{\renewcommand{\currentcourse}{#1}}
\makeatletter
\newcommand{\coursename}{\@nameuse{course@\currentcourse @name}}
\newcommand{\courseteacher}{\@nameuse{course@\currentcourse @teacher}}
% \setcourse{<course>}{<name>}{<teacher}
\newcommand{\setcourse}[3]{%
  \@namedef{course@#1@name}{#2}%
  \@namedef{course@#1@teacher}{#3}%
}
\makeatother

\setcourse{math}{Mathematics}{Bill}
\setcourse{physics}{Physics}{Susan}

\setlength{\parindent}{0pt}% Just for this example
\begin{document}

\course{math}

Course: \coursename \par
Teacher: \courseteacher

\course{physics}

Course: \coursename \par
Teacher: \courseteacher

\end{document}

One can build in error-checking, if needed.

Werner
  • 603,163