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?

2
  • \resizebox around the equation environment doesn't. You could box that up, but this would most likely destroy the effects of \resizebox
    – Skillmon
    Aug 31, 2017 at 8:26
  • What do you mean to get with \resizebox?
    – egreg
    Aug 31, 2017 at 8:45

3 Answers 3

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

1
  • I should also add that the $ notation is necessary for mathmode, e.g. $\alpha$. Aug 31, 2017 at 10:18
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

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

You must log in to answer this question.

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