5

I am using the nicematrix package to typeset a matrix with border (exterior) entries. Currently my code is like this:

\documentclass[preview]{standalone}
\usepackage{nicematrix}

\begin{document}
$\begin{pNiceMatrix}[first-row,last-row=5,first-col,last-col=5]
& C_1 & C_2 & C_3 & C_4 & \\
L_1 & a_{11} & a_{12} & a_{13} & a_{14} & L_1 \\
L_2 & a_{21} & a_{22} & a_{23} & a_{24} & ---L_2--- \\
L_3 & a_{31} & a_{32} & a_{33} & a_{34} & L_3 \\
L_4 & a_{41} & a_{42} & a_{43} & a_{44} & L_4 \\
& C_1 & C_2 & C_3 & C_4 &
\end{pNiceMatrix}$

\end{document}

enter image description here

BUT, I cannot get the last (border) column's entries to center align: in the right exterior column, L_2 is not aligned with the other entries in the same column. Is there a way to do it?

2
  • 1
    Welcome to TeX.SE!
    – Zarko
    Commented Mar 8, 2020 at 22:42
  • The left alignment of the last column (outside the fence) is hardwired in the package.
    – egreg
    Commented Mar 9, 2020 at 0:08

2 Answers 2

6

The final outer column is left aligned; the behavior is hardwired in the package, so it cannot be overridden.

However, there's eqparbox that can help. I define a helper \matheqbox command for the purpose; the optional argument (not used here, default c) is for the alignment inside the constructed boxes; the first mandatory argument is a label that must be unique for each application; the second mandatory argument is the box contents.

\documentclass{article}
\usepackage{nicematrix}
\usepackage{eqparbox}

\newcommand{\matheqbox}[3][c]{\eqmakebox[#2][#1]{$#3$}}

\begin{document}
\[
\begin{pNiceMatrix}[
  first-row,
  last-row=5,
  first-col,
  last-col=5,
]
& C_1 & C_2 & C_3 & C_4 & \\
L_1 & a_{11} & a_{12} & a_{13} & a_{14} & \matheqbox{a}{L_1} \\
L_2 & a_{21} & a_{22} & a_{23} & a_{24} & \matheqbox{a}{L_2+L_2+L_2} \\
L_3 & a_{31} & a_{32} & a_{33} & a_{34} & \matheqbox{a}{L_3} \\
L_4 & a_{41} & a_{42} & a_{43} & a_{44} & \matheqbox{a}{L_4} \\
& C_1 & C_2 & C_3 & C_4 &
\end{pNiceMatrix}
\]

\end{document}

enter image description here

5

Here is a way to obtain such matrix by putting the parenthesis in the preamble of an environment {NiceArray}.

\documentclass{article}
\usepackage{nicematrix}

\begin{document}

$\begin{NiceArray}[first-row,last-row]{c(cccc)c}
& C_1 & C_2 & C_3 & C_4 & \\
L_1 & a_{11} & a_{12} & a_{13} & a_{14} & L_1 \\
L_2 & a_{21} & a_{22} & a_{23} & a_{24} & ---L_2--- \\
L_3 & a_{31} & a_{32} & a_{33} & a_{34} & L_3 \\
L_4 & a_{41} & a_{42} & a_{43} & a_{44} & L_4 \\
& C_1 & C_2 & C_3 & C_4 
\end{NiceArray}$

\end{document}

Output of the first code


Here is another method with the command \SubMatrix in the \CodeAfter.

\documentclass{standalone}
\usepackage{nicematrix}

\begin{document}
$\begin{NiceMatrix}
& C_1 & C_2 & C_3 & C_4 & \\
L_1 & a_{11} & a_{12} & a_{13} & a_{14} & L_1 \\
L_2 & a_{21} & a_{22} & a_{23} & a_{24} & ---L_2--- \\
L_3 & a_{31} & a_{32} & a_{33} & a_{34} & L_3 \\
L_4 & a_{41} & a_{42} & a_{43} & a_{44} & L_4 \\
& C_1 & C_2 & C_3 & C_4 
\CodeAfter \SubMatrix({2-2}{5-5})
\end{NiceMatrix}$

\end{document}

You need several compilations (because nicematrix uses PGF/Tikz nodes).

Output of the second code

You must log in to answer this question.

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