Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

I want to have a command that accepts 1 optional argument and 3 mandatory ones.

Example:

  1. \qe{a}{b}{c} produces ax^2+bx+c
  2. \qe[y]{-1}{0}{-c} produces -y^2-c

How to make this command?

share|improve this question

3 Answers 3

up vote 12 down vote accepted
\documentclass{article}
\usepackage{polynomial}

\begin{document}
$\polynomial{\frac12,0,-1,1}$

$\polynomial[reciprocal]{\frac12,0,-1,1}$
\end{document}

alt text

See manual of polynomial for more options.

http://www.ctan.org/tex-archive/help/Catalogue/entries/polynomial.html

share|improve this answer
    
I think this one should be the accepted answer. –  Ryan Reich Jan 22 '11 at 16:37
    
Worth also to have a look at ctan.org/tex-archive/macros/latex/contrib/cool –  Yiannis Lazarides Jan 22 '11 at 16:55
    
@Yiannis: Yes. cool package is rather useful for those who use special math functions heavily. However, it doesn't deal with such polinomials. –  Leo Liu Jan 22 '11 at 17:22
\documentclass{article}
\usepackage{amsmath}

\def\MinusOne{-1}
\newcommand\qec[4][x]{%
  \ifx\relax#2\relax\else\def\temp{#2}
    \ifx\temp\MinusOne -\else #2\fi#1^2\fi
  \ifx\relax#3\relax\else\def\temp{#3}
    \ifx\temp\MinusOne -\else+#3\fi#1\fi
  \ifx\relax#4\relax\else+ #4\fi}
\begin{document}

\begin{align}
\qec{a}{b}{c}\\
\qec[y]{-1}{}{-c}\\
\qec[y]{-1}{}{}\\
\qec{}{1}{2}
\end{align}

\end{document}
share|improve this answer
    
@xport: see edit. However, it makes no real sense, because it becomes more and more complecated. +-c and +1x is also not a nice output. It is easier to type it as it is ... –  Herbert Jan 22 '11 at 16:24
    
The only one of your test cases which actually looks right is the first. –  Ryan Reich Jan 22 '11 at 16:36
    
@Ryan, edited the code –  Herbert Jan 22 '11 at 17:24

what about what follows as a first attempt:

\documentclass{book}
\newcommand{\qec}[4]{#1 #4^2 + #2 #4+ #3}
\begin{document}
\begin{equation}
\qec{a}{b}{c}{y}
\end{equation}
\begin{equation}
\qec{1}{2}{3}{p}
\end{equation}
\end{document}
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.