13

I currently taking a course in Algebra, and I find myself typing the polynomial

$a_nx^n+\ldots+a_1x+a_0$ 

over and over again, and I was wondering if I could create a shortcut for such a polynomial form, such that I can control what coefficients and variables I want.

I know the polynomial package exists, but I cannot seem to incorporate the "ldots" in the commands it offers.

  • Welcome to TeX.SE! – Mico Jan 25 at 14:14
  • Please tell us more about the "canonical form" of the polynomials you find yourself writing repeatedly. E.g., is the highest order always n (w/ n>1, right?) and is the lowest order always 0 , i.e., a constant? – Mico Jan 25 at 14:16
  • 2
    Exactly as you say! and thank you for the warm welcome :) @Mico – Kam Jan 25 at 14:16
  • Of course, the correct form for a polynomial is $(\cdots(a_nx+a_{n-1})x+\cdots+a_1)x+a_0$ ;-) – John Kormylo Jan 25 at 18:14
14

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).

enter image description here

\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.

enter image description here

\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}
  • 2
    Thank you so much!!! This is great :) (I would upvote, but I need 15 rep pts haha, as soon as I get them I'll take care of it! – Kam Jan 25 at 14:29
  • Question, if I want to change the variable "n", how should I proceed? I am sorry to bother you again – Kam Jan 25 at 14:36
  • @Kam - Please see the addendum I just posted. In this addendum, I changed the structure of the \pn macro so that it takes, in addition to the usual two mandatory arguments, an optional argument (to denote the highest order of the polynomial) whose default value is n. – Mico Jan 25 at 14:54
  • 1
    +1 for generating enthusiasm :) – user4686 Jan 25 at 15:59
  • Re the dots, just \usepackage{amsmath} and use \dots, which will automatically put the dots at the correct level for the adjacent operators. – David Richerby Jan 25 at 22:25
13

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]}

enter image description here

  • +1 for "fairly simple syntax". :-) – Mico Jan 25 at 23:17
  • 2
    @Mico Fairly simple user syntax. – egreg Jan 25 at 23:19
  • Thank you for taking the time to answer my post! I will definitely look into this as well :) where might you suggest I start properly learning about writing in Latex? I'm bewildered by what it seems to offer! – Kam Jan 26 at 5:50
1

I would propose \poly{ax^n}

\newcommand\poly[1]{\dopoly#1^n^\relax}
\def\dopoly#1#2^#3^#4\relax{#1_{#3}#2^{#3} + \dots + #1_{1}#2 + #1_{0}}

You can use \poly{ax} or \poly{ax^n}.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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