1

I want to write a matrix with equal column widths, plus have a vertical line down the matrix. By using the solutions given here and here, I have

\documentclass{article}
\usepackage{geometry}
\usepackage{amsmath}
\usepackage{graphicx}

% draw vertical line down matrix
\makeatletter
\renewcommand*\env@matrix[1][*\c@MaxMatrixCols c]{%
  \hskip -\arraycolsep
  \let\@ifnextchar\new@ifnextchar
  \array{#1}}
\makeatother

\begin{document}

\resizebox{\linewidth}{!}{%
\begin{equation}
\begin{pmatrix}[cc|cc]
 C1 & Column2 & C3 & C4 \\ \hline
 C5 & C6 & C7 & Column8
\end{pmatrix}$%
\end{equation}
}

\end{document}

The output matrix does not have equal column widths, and I get an error Missing $ inserted. How can I solve this problem?

3 Answers3

2

Notes:

  1. The error Missing $ inserted is because of the single $ sign at \end{pmatrix}$%.
  2. You can use \resizebox{.9\hsize}{!}{% inside the equation (see Scale an equation to fit exact page width).
  3. I used tabularx to define a new columntype C, witch allows you to set the width of a centered entry by using C{<width>}.

Code:

\documentclass{article}
\usepackage{geometry}
\usepackage{amsmath}
\usepackage{graphicx}

% draw vertical line down matrix
\makeatletter
\renewcommand*\env@matrix[1][*\c@MaxMatrixCols c]{%
  \hskip -\arraycolsep
  \let\@ifnextchar\new@ifnextchar
  \array{#1}}
\makeatother

\usepackage{tabularx}
\newcolumntype{C}[1]{>{\hspace{0pt}\centering\arraybackslash}p{#1}}

\begin{document}

\begin{equation}
    \resizebox{.9\hsize}{!}{\ensuremath{
        \begin{pmatrix}[*2{C{15mm}}|*2{C{15mm}}]
            C1 & Column2 & C3 & C4 \\ \hline
            C5 & C6 & C7 & Column8
        \end{pmatrix}%
    }}
\end{equation}

\end{document}

Result:

enter image description here

dexteritas
  • 9,161
1
  • in your mwe you have superfluous $ on the end of pmatrix
  • instead of hacked pmatrix i would rather use standard array environment (see difference at horizontal line)

\documentclass{article}
\usepackage{geometry}
\usepackage{amsmath}
\usepackage{graphicx}

% draw vertical line down matrix
\makeatletter
\renewcommand*\env@matrix[1][*\c@MaxMatrixCols c]{%
  \hskip -\arraycolsep
  \let\@ifnextchar\new@ifnextchar
  \array{#1}}
\makeatother

\usepackage{array}
\newcolumntype{C}[1]{>{\centering\arraybackslash}p{#1}}

\begin{document}

your solution (corrected and without \verb+\resizebox+):
\begin{equation}
\begin{pmatrix}[cc|cc]
 C1 & Column2 & C3 & C4 \\ \hline
 C5 & C6 & C7 & Column8
\end{pmatrix}%
\end{equation}

with proposed use of \verb+array+:
\begin{equation}\setlength\arraycolsep{1.5pt}
\left(\begin{array}{C{4em}C{4em}|C{4em}C{4em}}
 C1 & Column2 & C3 & C4 \\ \hline
 C5 & C6 & C7 & Column8
\end{array}\right)%
\end{equation}

\end{document}

enter image description here

Zarko
  • 296,517
0

The environment {pNiceMatrix} of nicematrix has a key columns-width=auto which specify that all the columns must have the same width.

\documentclass{article}
\usepackage{nicematrix}

\begin{document}

[\begin{pNiceArray}{cc|cc}[columns-width=auto,margin] C1 & Column2 & C3 & C4 \ \hline C5 & C6 & C7 & Column8 \end{pNiceArray}]

\end{document}

Output of the above code

F. Pantigny
  • 40,250