2

Based on Leo's answer but I removed one column and replaced the \big) with a vertical rule. Now I want to make all columns have the same width. How to do this?

enter image description here

\documentclass{article}
\usepackage{amssymb}

\begin{document}

\[
x^3 - x + 1 = (x-1)(x^2+x) + 1 \in \mathbb{F}_3[x]
\]

\[
\renewcommand\arraystretch{1.2}
\begin{array}{*{6}{r}}
&
    &
        &
            1&
                1&
                    0\\\cline{3-6}
1&
    -1&
        \multicolumn{1}{|r}{1}&
                                0&
                                    -1&
                                        1\\
&
    & 
        1&
            -1&
                &
                    \\\cline{3-5}
&
    &
        &
            1&
                -1&
                    \\
&
    &
        &
            1&
                -1&
                    \\\cline{4-6}
&
    &
        &
            &
                &
                    1\\
\end{array}
\]

\end{document}

I got the solution by using \begin{array}{*{6}{>{\hfill}m{1cm}}}, but is there any other solution?

David Carlisle
  • 757,742
Display Name
  • 46,933

3 Answers3

2
\documentclass{article}
\usepackage{amssymb,array,ragged2e}
\newcolumntype{R}[1]{>{\RaggedLeft}p{#1}}
\begin{document}

\[ x^3 - x + 1 = (x-1)(x^2+x) + 1 \in \mathbb{F}_3[x] \]

\[
\renewcommand\arraystretch{1.2}
\begin{array}{*6{R{1em}}}
...
1

I would prefer, but it's a question of taste, the following:

\usepackage{array}
\newcolumntype{R}[1]{>{\hbox to #1\bgroup\hfill$}c<{$\egroup}}

...
\[
\renewcommand\arraystretch{1.2}
\newcommand\x[1]{\multicolumn{1}{|r}{#1}} % just not to clutter the array
\begin{array}{*{6}{R{1.5em}}}
  &    &       & 1  &  1 & 0 \\\cline{3-6}
1 & -1 & \x{1} & 0  & -1 & 1 \\
  &    &    1  & -1 &    &   \\\cline{3-5}
  &    &       &  1 & -1 &   \\
  &    &       &  1 & -1 &   \\\cline{4-6}
  &    &       &    &    & 1 
\end{array}
\]

The syntax \hbox to <dimen> is low level TeX, which is generally not recommended in LaTeX because it can have disastrous effects on color management. However, since this is done at the start at the cell, it should not be a problem. The construction \hbox to 1.5em{\hfil x} is similar to \makebox[1.5em][r]{x}, but it can be split into two parts: \hbox to 1.5em\bgroup at the beginning and \egroup at the end, so the "argument" can be gathered with array's syntax.

A similar setup can be obtained by

\newcolumntype{R}[1]{>{\raggedleft\arraybackslash$}p{#1}<{$}}
David Carlisle
  • 757,742
egreg
  • 1,121,712
1

With {NiceArray} of nicematrix, you have a key columns-width. When this key is used with the value auto, all the columns of the array have the same width, equal to the width of the widest cell of the array.

\documentclass{article}
\usepackage{amssymb}
\usepackage{nicematrix}

\begin{document}

[ x^3 - x + 1 = (x-1)(x^2+x) + 1 \in \mathbb{F}_3[x] ]

[ \renewcommand\arraystretch{1.2} \begin{NiceArray}{rrrrrr}[columns-width=auto] & & & 1& 1& 0\\cline{3-6} 1& -1& \multicolumn{1}{|r}{1}& 0& -1& 1 \ & & 1& -1& & \\cline{3-5} & & & 1& -1& \ & & & 1& -1& \\cline{4-6} & & & & & 1\ \end{NiceArray} ]

\end{document}

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

Output of the above code

F. Pantigny
  • 40,250