答案1
检查各种情况;在这种情况\ifnum
下比处理起来容易得多\ifthenelse
。
\documentclass{article}
\newcommand{\printeq}[2]{%
\ensuremath{
y=
\ifnum#1=0 % no slope part
#2
\else
\ifnum#1=1
\else
\ifnum#1=-1
-
\else
#1
\fi
\fi x
\ifnum#2>0
+#2
\else
\ifnum#2=0
\else
#2
\fi
\fi
\fi
}%
}
\begin{document}
\printeq{-6}{-5}
\printeq{-6}{0}
\printeq{-6}{7}
\printeq{-1}{-5}
\printeq{-1}{0}
\printeq{-1}{7}
\printeq{0}{-5}
\printeq{0}{0}
\printeq{0}{7}
\printeq{1}{-5}
\printeq{1}{0}
\printeq{1}{7}
\printeq{8}{-5}
\printeq{8}{0}
\printeq{8}{7}
\end{document}
答案2
\documentclass{article}
\edef\paa{\the\numexpr 1+\pdfuniformdeviate 9}
\edef\naa{\the\numexpr 1+\pdfuniformdeviate 9}
\edef\pll{\the\numexpr 1+\pdfuniformdeviate 9}
\edef\nll{\the\numexpr 1+\pdfuniformdeviate 9}
\begin{document}
\begin{tabular}{l|lll}
&$l<0$ say $l=\nll$& $l=0$&$l>0$ say $l=\pll$\\\hline
$a<0$ say $a=-\naa$& $y=-\nll x-\naa$ & $y=-\naa$&$y=\pll x-\naa$\\
$a=0$ & $y=-\nll x$ & $y=0$ &$y=\pll x$ \\
$a>0$ say $a=\paa$ & $y=-\nll x+\paa$ & $y=\paa$ &$y=\pll x+\paa$\\
\end{tabular}
or if you just want one of the 9, then
\ifcase \pdfuniformdeviate 10
$y=-\nll x-\naa$ \or $y=-\naa$\or$y=\pll x-\naa$\or
$y=-\nll x$ \or $y=0$ \or$y=\pll x$ \or
$y=-\nll x+\paa$ \or $y=\paa$ \or$y=\pll x+\paa$\fi
\end{document}