GeneralUsing output of one (new)command as input of another

LaTeX specific issues not fitting into one of the other forums of this category.
Post Reply
pkledgrape
Posts: 4
Joined: Sat May 07, 2011 7:00 pm

Using output of one (new)command as input of another

Post by pkledgrape »

Eventually, I'm trying to draw playing cards using tikz; I can do this very nicely typing everything out every single time, but it would be nice to just have a command like

Code: Select all

\card{3}{H}
which would draw a 3 of hearts for me.

As such: I've defined two commands, \suit and \suitcolor, as follows:

Code: Select all

\DeclareSymbolFont{extraup}{U}{zavm}{m}{n}
\DeclareMathSymbol{\varheart}{\mathalpha}{extraup}{86}
\DeclareMathSymbol{\vardiamond}{\mathalpha}{extraup}{87}
\newcommand{\suit}[1]{
   \ifthenelse{\equal{#1}{H}}{\varheart}{}
   \ifthenelse{\equal{#1}{D}}{\vardiamond}{}
   \ifthenelse{\equal{#1}{C}}{\clubsuit}{}
   \ifthenelse{\equal{#1}{S}}{\spadesuit}{}
   }

Code: Select all

\newcommand{\suitcolor}[1]{
   \ifthenelse{\equal{#1}{H}}{red}{
      \ifthenelse{\equal{#1}{D}}{red}{black}
      }
   }
These seem to do exactly as I want on their own (see example below), but not as inputs to another function (again - see below)

Code: Select all

\documentclass{amsart}
\usepackage{tikz,ifthen}
\DeclareSymbolFont{extraup}{U}{zavm}{m}{n}
\DeclareMathSymbol{\varheart}{\mathalpha}{extraup}{86}
\DeclareMathSymbol{\vardiamond}{\mathalpha}{extraup}{87}
\newcommand{\suit}[1]{
   \ifthenelse{\equal{#1}{H}}{\varheart}{}
   \ifthenelse{\equal{#1}{D}}{\vardiamond}{}
   \ifthenelse{\equal{#1}{C}}{\clubsuit}{}
   \ifthenelse{\equal{#1}{S}}{\spadesuit}{}
   }
\newcommand{\suitcolor}[1]{
   \ifthenelse{\equal{#1}{H}}{red}{
      \ifthenelse{\equal{#1}{D}}{red}{black}
      }
   }
%%%-------------------------------------
\begin{document}
The command $\backslash$suit gives me exactly what I'd like:\\
\$$\backslash$suit\{H\}\$ gives $\suit{H}$\\
\$$\backslash$suit\{D\}\$ gives $\suit{D}$\\
\$$\backslash$suit\{C\}\$ gives $\suit{C}$\\
\$$\backslash$suit\{S\}\$ gives $\suit{S}$\\

Similarly, $\backslash$suitcolor returns the color of the suit as I'd hope:\\
$\backslash$suitcolor\{H\} gives \suitcolor{H}\\
$\backslash$suitcolor\{D\} gives \suitcolor{D}\\
$\backslash$suitcolor\{C\} gives \suitcolor{C}\\
$\backslash$suitcolor\{S\} gives \suitcolor{S}\\

But what I'd really like to be able to do is, with a single input, make (for example) a red heart.

I can easily do this with a bit of code:\\
\{$\backslash$color\{red\} \$$\backslash$suit\{H\}\$\} gives {\color{red} $\suit{H}$}

But something breaks down if I try to use $\backslash$suitcolor to define the color:\\
\{$\backslash$color\{$\backslash$suitcolor\{H\}\} \$$\backslash$suit\{H\}\$\} gives {\color{\suitcolor{H}} $\suit{H}$}\\
:( a black heart, not a red one as intended.
\end{document}
How can I get the output of the \suitcolor function to be usable as an input for \color?

Thanks!

Recommended reading 2024:

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

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

Post Reply