2

Based on the answer to this question, I've written a macro that returns a dot product with the top of the column array lined up properly with the top of the row array.

\documentclass{amsart}
\newcommand{\myDotProduct}[3]{
        \begin{array}{@{}c@{}}
         \begin{bmatrix}
            #1
         \end{bmatrix}  #3
        \end{array}
        \begin{bmatrix}
            #2
        \end{bmatrix}
        \mathstrut
        }
\begin{document}
\begin{align*}
\myDotProduct{a & b}{z_1 \\ z_2}{\\ \\}
\end{align*}
\end{document}

It would be much more satisfactory if I could eliminate the third argument, by counting the number of \\'s that appear in the second argument, then using \expr to add one to this count, and then inserting the required number of \\'s in the appropriate place. But

  1. I don't know how to count the number of occurrences of \\ in the second argument

  2. I don't know how to implement the concept: output n replicas of \\

If there's no answer to (1) but an answer to (2), then a second best solution would be to replace the third argument {\\ \\} in the above macro with {2}, then use the answer to (2) to do the rest.

Leo Simon
  • 2,199

2 Answers2

3

The aspect is horrible, but you're the judge. Instead of doing complicated things you can use delarray:

\documentclass{amsart}
\usepackage{delarray}

\makeatletter
\newcommand{\myDotProduct}[2]{%
  \begin{array}{@{}c@{}}
  \begin{bmatrix}#1\end{bmatrix}%
  \begin{array}[t]\lbrack{@{}c@{}}\rbrack#2\end{array}
  \end{array}
}
\makeatother

\begin{document}

\begin{equation*}
x=\myDotProduct{a & b}{z_1 \\ z_2}+
\myDotProduct{a & b & c}{u \\ v \\ w}
\end{equation*}

\end{document}

The outer array ensures global vertical centering.

enter image description here

If you want to go with the counting:

\documentclass{amsart}
\usepackage{xparse}

\ExplSyntaxOn
\NewDocumentCommand{\myDotProduct}{mm}
 {
  \seq_set_split:Nnn \l_tmpa_seq { \\ } { #2 }
  \begin{array}{@{}c@{}}
  \begin{bmatrix} #1 \end{bmatrix}
  \prg_replicate:nn { \seq_count:N \l_tmpa_seq } { \\ }
  \end{array}
  \begin{bmatrix} #2 \end{bmatrix}
  \mathstrut
 }
\ExplSyntaxOff

\begin{document}
\begin{equation*}
x=\myDotProduct{a & b}{z_1 \\ z_2}+
\myDotProduct{a & b & c}{u \\ v \\ w}
\end{equation*}
\end{document}

The function \seq_count:N \l_tmpa_seq returns the items in the second arguments (taking \\ as separator).

egreg
  • 1,121,712
  • I'm going to go with @Werner's adjustbox suggestion, which seems very simple, but it's really helpful to know how to count things in tex, Thanks – Leo Simon Sep 11 '16 at 01:40
2

What about something else? Instead of inserting an appropriate number of \\ as part of the first matrix, align the second matrix at the top using adjustbox:

enter image description here

\documentclass{amsart}

\usepackage{adjustbox}

\newcommand{\myDotProduct}[3]{%
  \begin{array}{@{}c@{}}
    \begin{bmatrix}
      #1
    \end{bmatrix}  #3
  \end{array}
  \begin{bmatrix}
    #2
  \end{bmatrix}
  \mathstrut
  }

\newcommand{\newDotProduct}[2]{%
  \begin{bmatrix}
    #1
  \end{bmatrix}
  \adjustbox{valign=t}{$\begin{bmatrix}
    #2
  \end{bmatrix}$}
}

\begin{document}

\[
  \myDotProduct{a & b & c}{z_1 \\ z_2 \\ z_3 \\ z_4}{\\ \\ \\ \\}
\]

\[
  \newDotProduct{a & b & c}{z_1 \\ z_2 \\ z_3 \\ z_4}
\]

\end{document}
Werner
  • 603,163