Shortcut for a polynomial of the form $a_nx^n+\ldots+a_1x+a_0$
I think that what you need is a macro that takes two arguments: the "name" of the coefficients, and the "name" of the base of the power terms. The names will, in general, be single letters, right? (You've indicated, in a comment, that the highest and lowest order of the polynomial are always n
and 0
, respectively.) The macro called \pn
in the following example satisfies these criteria.
Incidentally, the typographic ellipsis used between binary operators (such as +
) is usually of the form \cdots
, not \ldots
. (The letters "c" and "l" refer to either centered (on the math line) or low (on the typographic baseline).
\documentclass{article}
%% The following macro must be used only in math mode:
\newcommand\pn[2]{#1_n #2^n + \cdots + #1_1 #2 + #1_0}
\begin{document}
$\pn{a}{x}$
$\pn{\lambda}{z}$
$\pn{\alpha}{\xi}$
\end{document}
Addendum to address the OP's follow-up request: Suppose that not all polynomials are of order n
, but that it's true that most polynomials are, in fact, order n
. In that case, it makes sense to modify the \pn
macro that it takes 3 rather than 2 arguments, with additional argument taking on the value n
by default.
\documentclass{article}
%% The following macro must be used only in math mode:
\newcommand\pn[3][n]{#2_{#1} #3^{#1} + \cdots + #2_1 #3 + #2_0}
\begin{document}
$\pn{a}{x}$ % use default order (n) of polynomial
$\pn[4]{\lambda}{z}$
$\pn[q]{\alpha}{\xi}$
\end{document}
With a fairly simple syntax:
\documentclass{article}
\usepackage{amsmath}
\usepackage{xparse}
\ExplSyntaxOn
\NewDocumentCommand{\poly}{O{}}
{
\group_begin:
\keys_set:nn { poly } { #1 }
\kam_poly:
\group_end:
}
\keys_define:nn { poly }
{
degree .tl_set:N = \l__poly_degree_tl,
var .tl_set:N = \l__poly_var_tl,
coef .tl_set:N = \l__poly_coef_tl,
reverse .bool_set:N = \l__poly_reverse_bool,
degree .initial:n = n,
var .initial:n = x,
coef .initial:n = a,
reverse .default:n = true,
}
\cs_new_protected:Nn \kam_poly:
{
\bool_if:NTF \l__poly_reverse_bool
{
\l__poly_coef_tl \sb { 0 } +
\l__poly_coef_tl \sb { 1 } \l__poly_var_tl +
\dots +
\l__poly_coef_tl \sb { \l__poly_degree_tl }
\l__poly_var_tl \sp { \l__poly_degree_tl }
}
{
\l__poly_coef_tl \sb { \l__poly_degree_tl }
\l__poly_var_tl \sp { \l__poly_degree_tl } +
\dots +
\l__poly_coef_tl \sb { 1 } \l__poly_var_tl +
\l__poly_coef_tl \sb { 0 }
}
}
\ExplSyntaxOff
\begin{document}
$\poly$
$\poly[var=z]$
$\poly[var=t,degree=m,coef=b]$
$\poly[var=t,degree=m,coef=b,reverse]$
\end{document}
The keys can be specified in any order, freeing you from the need to remember which parameter goes first; the default values are
var = x
degree = n
coef = a
You can also make shorthands with, say
\newcommand{\polybtn}{\poly[var=t,coef=b,degree=n]}