# How to add index next to a matrix

That is my actual matrix :

And this is my code :

P_{i, j} = \begin{pmatrix}
1 & 0 & 0 & 0 & 0 & 0 & 0 & 0 \\
0 & \ddots & \vdots & \vdots & \vdots & \vdots & \vdots & \vdots \\
\vdots & 0 & 0 & \dots & \dots & 1 & \vdots & \vdots & (\text{ligne } i)\\
\vdots & \vdots & \vdots & 1 & 0 & 0 & \vdots & \vdots \\
\vdots & \vdots & \vdots & 0 & \ddots & \vdots & \vdots & \vdots \\
\vdots & \vdots & 1 & 0 & \dots & 0 & \dots & 0 & (\text{ligne } j) \\
\vdots & \vdots & 0 & \vdots & 0 & \vdots & 1 & 0 \\
0 & 0 & 0 & 0 & 0 & 0 & 0 & 1
\end{pmatrix}


I would like the text to be next to the matrix, outside the brackets, but at the same height than now.

• Welcome to TeX.SE! Please make your code snippet be compilable! Then we do not have to guess what you are doing ... Commented Aug 18, 2022 at 16:14

With the package nicematrix, you only have to change your environment {pmatrix} in {pNiceMatrix} and add the key last-col.

\documentclass{article}
\usepackage{nicematrix}

\begin{document}

$P_{i, j} = \begin{pNiceMatrix}[last-col] 1 & 0 & 0 & 0 & 0 & 0 & 0 & 0 \\ 0 & \ddots & \vdots & \vdots & \vdots & \vdots & \vdots & \vdots \\ \vdots & 0 & 0 & \dots & \dots & 1 & \vdots & \vdots & (\text{ligne } i)\\ \vdots & \vdots & \vdots & 1 & 0 & 0 & \vdots & \vdots \\ \vdots & \vdots & \vdots & 0 & \ddots & \vdots & \vdots & \vdots \\ \vdots & \vdots & 1 & 0 & \dots & 0 & \dots & 0 & (\text{ligne } j) \\ \vdots & \vdots & 0 & \vdots & 0 & \vdots & 1 & 0 \\ 0 & 0 & 0 & 0 & 0 & 0 & 0 & 1 \end{pNiceMatrix}$

\end{document}


Here is another way to represent that matrix of transposition (also with nicematrix).

\documentclass{article}
\usepackage{nicematrix,tikz}

\begin{document}

\NiceMatrixOptions{code-for-first-row = \scriptstyle,code-for-first-col = \scriptstyle }
\setcounter{MaxMatrixCols}{12}
\newcommand{\blue}{\color{blue}}
$P_{i,j} = \begin{pNiceMatrix}[last-row,last-col,nullify-dots,xdots/line-style={dashed,blue}] 1& & & \Vdots & & & & \Vdots \\ & \Ddots[line-style=standard] \\ & & 1 \\ \Cdots[color=blue,line-style=dashed]& & & \blue 0 & \Cdots & & & \blue 1 & & & \Cdots & \blue \leftarrow i \\ & & & & 1 \\ & & &\Vdots & & \Ddots[line-style=standard] & & \Vdots \\ & & & & & & 1 \\ \Cdots & & & \blue 1 & \Cdots & & \Cdots & \blue 0 & & & \Cdots & \blue \leftarrow j \\ & & & & & & & & 1 \\ & & & & & & & & & \Ddots[line-style=standard] \\ & & & \Vdots & & & & \Vdots & & & 1 \\ & & & \blue \overset{\uparrow}{i} & & & & \blue \overset{\uparrow}{j} \\ \end{pNiceMatrix}$

\end{document}


You need several compilations (because of the PGF/Tikz nodes used by nicematrix).

• I really enjoy each time you, @F. Pantigny, answer a question using nicematrix. +1.
– FHZ
Commented Aug 18, 2022 at 17:50
• thank you very much! Commented Aug 18, 2022 at 17:56

Without any extra package, you have to add another adjacent matrix and make sure the vertical spacing in that matrix is the same as in the left one. It takes a little work. One way to do so is to use \vphantom{}, which doesn't typeset anything, doesn't take horizontal space at all, but still let LaTeX calculate occupied vertical space of its argument.

I would also base the solution on array instead of bmatrix, pmatrx, or matrix. It gives more control over alignments and spacing, still with possibility to add scaled brackets of any type.

\documentclass[journal]{IEEEtran}
\usepackage{array}
\usepackage{mleftright}
\usepackage{mathtools}

\AtBeginDocument{\mleftright}
\setlength\arraycolsep{6pt}

\begin{document}

$P_{i, j} = \left( % Auto-scaled open bracket \begin{array}{ @{\hspace{6pt}} % set space between left bracket and the first column *8{c} @{\hspace{6pt}}} % as above but set space between the last column and the right bracket 1 & 0 & 0 & 0 & 0 & 0 & 0 & 0 \\ 0 & \ddots & \vdots & \vdots & \vdots & \vdots & \vdots & \vdots \\ \vdots & 0 & 0 & \dots & \dots & 1 & \vdots & \vdots \\ \vdots & \vdots & \vdots & 1 & 0 & 0 & \vdots & \vdots \\ \vdots & \vdots & \vdots & 0 & \ddots & \vdots & \vdots & \vdots \\ \vdots & \vdots & 1 & 0 & \dots & 0 & \dots & 0 \\ \vdots & \vdots & 0 & \vdots & 0 & \vdots & 1 & 0 \\ \vphantom{\vdots} 0 & 0 & 0 & 0 & 0 & 0 & 0 & 1 \end{array} \right) % Auto-scaled closing bracket \hspace{1em} % space between two arrays \begin{array}{ @{} % set no space between columns >{\vphantom{\vdots}} % ensures the vertical spacing l @{} % - the same as above l @{} % - the same as above } \multicolumn{1}{@{}c@{}}{} & \\% cancels an effect of \vphantom{\vdots} in this row & \\ & (\text{ligne } i)\\ & \\ & \\ & (\text{ligne } j) \\ & \\ & \\ \end{array}$
\end{document}


This code is not the better one but it works without new package

$P_{i, j} = \begin{pmatrix} 1 & 0 & 0 & 0 & 0 & 0 & 0 & 0 \\ 0 & \ddots & \vdots & \vdots & \vdots & \vdots & \vdots & \vdots \\ \vdots & 0 & 0 & \dots & \dots & 1 & \vdots & \vdots &\\ \vdots & \vdots & \vdots & 1 & 0 & 0 & \vdots & \vdots \\ \vdots & \vdots & \vdots & 0 & \ddots & \vdots & \vdots & \vdots \\ \vdots & \vdots & 1 & 0 & \dots & 0 & \dots & 0 & \\ \vdots & \vdots & 0 & \vdots & 0 & \vdots & 1 & 0 \\ 0 & 0 & 0 & 0 & 0 & 0 & 0 & 1 \end{pmatrix} \begin{matrix} \vphantom{0}\\ \vphantom{\vdots}\\ \vphantom{\vdots}(\text{ligne } i)\\ \vphantom{\vdots}\\ \vphantom{\vdots}\\ \vphantom{\vdots}(\text{ligne } j)\\ \vphantom{\vdots}\\ \vphantom{0}\\ \end{matrix}$

• I am not an expert, but please post your MWE from \documentclass ...\end{document} as always... Commented Aug 18, 2022 at 16:48