# How can I typeset vertical addition (or multiplication) on things other than plain numbers, such as polynomials?

tl;dr My problem is finely positioning the operation sign.

In the examples given elsewhere (here and here, for instance), two ways of typesetting vertical arithmetic operations are given:

1. Macros from xlop package, such as opmul.
2. matrix or tabular environment.

The downside of xlop is that no fine tuning is possible, and the built in functionality is not fit for my use cases.

With matrix or tabular, the problem is positioning the operation sign. In all the examples I could find, the sign is placed in its own column on the left of the second of two summands. However, I would like it to be centered vertically with respect to the block of summands — so with even number thereof the sign must be positioned somewhat inbetween the lines.

If I write the operation sign before the matrix environment, like this:

$$+ \quad \begin{matrix} 2 & 3 & 5 & 7 \\ & 11 & 13 & 17 \\ \hline \\ \end{matrix}$$


— It will be positioned on the vertical centerline of the box:

— But if I try to add another line, or another single line matrix (for the result), it will go awry:

$$+ \quad \begin{matrix} 2 & 3 & 5 & 13 \\ & 11 & 7 & 17 \\ \hline 2 & 14 & 12 & 30 \end{matrix}$$


(The operation sign is out of vertical alignment.)

$$+ \quad \begin{matrix} 2 & 3 & 5 & 13 \\ & 11 & 7 & 17 \\ \hline \end{matrix}$$ $$\begin{matrix} 2 & 14 & 12 & 30 \end{matrix}$$


(The result is out of horizontal alignment.)

What can be done?

• "...But if I try to add another line, or another single line matrix..." -- Please be more specific about what you're trying to achieve. – Mico Jun 25 '18 at 16:36
• @Mico I added the examples concerning the line you have quoted. Is it clear now? – Ignat Insarov Jun 25 '18 at 17:05

Is it this layout you want?

Unrelated : don't use the plain TeX syntax $$...$$ for displayed equations, use LaTeX syntax $...$.

\documentclass{article}
\usepackage{amsmath}
\usepackage{array, multirow}
\usepackage{booktabs}

\begin{document}

$\begin{matrix} \multirow{2}{*} { + }& 2 & 3 & 5 & 13 \\ & & 11 & 7 & 17 \\ \cmidrule{2-5} & 2 & 14 & 12 & 30 \end{matrix}$%

\end{document}


• Thank you! I should look into advanced tabulation packages. Also, \midrule looks much nicer than \hline. I'm very happy. – Ignat Insarov Jun 26 '18 at 1:22
• It looks nicer mainly because booktabs it adds some vertical padding around the rules. – Bernard Jun 26 '18 at 9:01