7

I have to label row and column index around matrix. I know blkarray would help, so I write like this:

\documentclass[a4paper,12pt]{article}

\usepackage{amsmath}
\usepackage{blkarray}% http://ctan.org/pkg/blkarray
\newcommand{\matindex}[1]{\mbox{#1}}% Matrix index

\begin{document}

pmatrix way:
\[
J(i,j,\theta)=\begin{pmatrix}
1 & \cdots & 0 & \cdots & 0 & \cdots & 0 \\
\vdots & \ddots & \vdots & & \vdots & & \vdots \\
0 & \cdots & c & \cdots & -s & \cdots & 0 \\
\vdots & & \vdots & \ddots & \vdots & & \vdots \\
0 & \cdots & s & \cdots & c & \cdots & 0 \\
\vdots & & \vdots & & \vdots & \ddots & \vdots \\
0 & \cdots & 0 & \cdots & 0 & \cdots & 1 
\end{pmatrix}
\]

blkarray way:
\[
  J(i,j,\theta)=\begin{blockarray}{cccccccc}
    \begin{block}{(ccccccc)c}
        1 & \cdots & 0 & \cdots & 0 & \cdots & 0 \\
        \vdots & \ddots & \vdots & & \vdots & & \vdots \\
        0 & \cdots & c & \cdots & -s & \cdots & 0 & \matindex{$i$} \\
        \vdots & & \vdots & \ddots & \vdots & & \vdots \\
        0 & \cdots & s & \cdots & c & \cdots & 0 & \matindex{$j$} \\
        \vdots & & \vdots & & \vdots & \ddots & \vdots \\
        0 & \cdots & 0 & \cdots & 0 & \cdots & 1 \\
    \end{block}
    & & \matindex{$i$} & & \matindex{$j$} & \\
  \end{blockarray}
\]

Why $J(i,j,\theta)$ isn't vertical aligned int blkarray way?
\end{document}

Here is the result: enter image description here I want to know why isn't vertical aligned in blkarray way.

And how to vertical aligned it like pmatrix way.

Thanks.

3
  • 2
    It is vertically aligned, but takes into aaccount the supplementary row with $i$ and $j$.
    – Bernard
    Jun 10, 2016 at 9:11
  • @Bernard: If you remove the extra row it isn't aligned either, and if you add \[-2cm] at the end the J(i,...) doesn't move. Jun 10, 2016 at 9:31
  • @Ulrike Fischer: I know blkarray has some problems with vertical centring, and I should have tested to see what happens really. Anyway, I have a solution with \raisebox and another with pst-node. Please see my post.
    – Bernard
    Jun 10, 2016 at 10:18

3 Answers 3

7

The problem is that the block array is vertically centered with respect to its entire size, including the indices below.

The easiest way, in this case, is to put the whole thing in the blkarray.

\documentclass[a4paper,12pt]{article}

\usepackage{amsmath}
\usepackage{blkarray}% http://ctan.org/pkg/blkarray

\begin{document}

\[
\begin{blockarray}{r@{}cccccccc}
\begin{block}{r(ccccccc)c}
                 & 1 & \cdots & 0 & \cdots & 0 & \cdots & 0 \\
                 & \vdots & \ddots & \vdots & & \vdots & & \vdots \\
                 & 0 & \cdots & c & \cdots & -s & \cdots & 0 & i \\
J(i,j,\theta)={} & \vdots & & \vdots & \ddots & \vdots & & \vdots \\
                 & 0 & \cdots & s & \cdots & c & \cdots & 0 & j \\
                 & \vdots & & \vdots & & \vdots & \ddots & \vdots \\
                 & 0 & \cdots & 0 & \cdots & 0 & \cdots & 1 \\
\end{block}
                 & & & i & & j & \\
\end{blockarray}
\]

\end{document}

enter image description here

4
  • wow, your solution is really elegant, thanks.
    – user1024
    Jun 10, 2016 at 10:58
  • I just found a problem too, what if the number of rows is even? which row should J(i,j,\theta)={} be placed to ?
    – user1024
    Jun 10, 2016 at 11:07
  • @egreg: See what happens if you have to add some maths after the blockarray, say \times A.
    – Bernard
    Jun 10, 2016 at 11:40
  • @Bernard Of course: I said “in this particular case”.
    – egreg
    Jun 10, 2016 at 11:42
6

You can obtain this result either with pmatrixand pstricks, defining certain elements as nodes, and attaching labels to these nodes, or with blockarray, stackengine or makecell, and a \raisebox command:

\documentclass[a4paper,12pt]{article}

\usepackage{mathtools}
\usepackage{blkarray, makecell,  stackengine}
\usepackage{pst-node, auto-pst-pdf} %

\begin{document}

\verb|pmatrix way + pstricks way:|\bigskip

\[
 \begin{postscript}
 J(i,j,\theta)=%
 \begin{pmatrix}
1 & \cdots & 0 & \cdots & 0 & \cdots & 0 \\
\vdots & \ddots & \vdots & & \vdots & & \vdots\\
0 & \cdots & c & \cdots & -s & \cdots &\rnode{rowi}{0}\\%
\vdots & & \vdots & \ddots & \vdots & & \vdots \\
0 & \cdots & s & \cdots & c & \cdots & \rnode{rowj}{0}\\%
\vdots & & \vdots & & \vdots & \ddots & \vdots \\
0 & \cdots & \rnode{coli}{0} & \cdots & \rnode{colj}{0} & \cdots & 1
\end{pmatrix}
 \psset{labelsep=1.2em}
\nput{0}{rowi}{i}\nput{0}{rowj}{j}
 \psset{labelsep=1.2ex}
\nput{-90}{coli}{i}\nput{-90}{colj}{j}
\end{postscript}
\]

\verb|blkarray + makecell or stackengine way:  |
\[
  J(i,j,\theta)= \raisebox{-0.3\baselineskip}{$ \begin{blockarray}{(*{7}{c})c}
        1 & \cdots & 0 & \cdots & 0 & \cdots & 0 \\
        \vdots & \ddots & \vdots & & \vdots & & \vdots \\
        0 & \cdots & c & \cdots & -s & \cdots & 0 & i\\ 
        \vdots & & \vdots & \ddots & \vdots & & \vdots \\
        0 & \cdots & s & \cdots & c & \cdots & 0 & j \\
        \vdots & & \vdots & & \vdots & \ddots & \vdots \\
        0 & \cdots & \smash{\stackunder[1.2ex]{0}{$ i $}} & \cdots &\smash{\makecell[tc]{0\\j}} & \cdots & 1
  \end{blockarray} $} 
\]%

 \end{document} 

enter image description here

3
  • Well thanks, It really works, but I have to adjust the -0.8 in other cases, It's not that elegant. Hope bug fixed.
    – user1024
    Jun 10, 2016 at 10:54
  • Yes, this value was found by trial and errors. Maybe someone knows the length that's involved in vertical spacing of rows?
    – Bernard
    Jun 10, 2016 at 10:57
  • @user1024: I've modified the code for the solution with blkarray. It simplifies the code (no more block).
    – Bernard
    Jun 10, 2016 at 11:35
1

With {pNiceMatrix} of nicematrix, you will have directly the expected result.

\documentclass[a4paper,12pt]{article}

\usepackage{nicematrix}

\begin{document}

\[
J(i,j,\theta)=\begin{pNiceMatrix}[last-row=8,last-col=8]
1 & \cdots & 0 & \cdots & 0 & \cdots & 0 \\
\vdots & \ddots & \vdots & & \vdots & & \vdots \\
0 & \cdots & c & \cdots & -s & \cdots & 0 & i \\
\vdots & & \vdots & \ddots & \vdots & & \vdots \\
0 & \cdots & s & \cdots & c & \cdots & 0 & j \\
\vdots & & \vdots & & \vdots & \ddots & \vdots \\
0 & \cdots & 0 & \cdots & 0 & \cdots & 1 \\
  & & i & & j & \\
\end{pNiceMatrix}
\]

\end{document}

Output of the above code

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .