[Tex/LaTex] Right-aligned columns using the tabularx environment

arrayshorizontal alignmentspacingtabularx

I'm trying to typeset a matrix which has entries of differing widths. However, for aesthetic reasons, I would like each of the columns of the matrix to be the same width, and I'd furthermore like them to be right-aligned.

I'm working in the tabularx environment (because of its ability to adjust column width uniformly). However, if I compile

\documentclass{amsart}
\usepackage{amsmath}
\usepackage{array}
\usepackage{tabularx}

\begin{document}

\[
\left[
\begin{tabularx}{1in}{XXX}
x & 0 & y\\
0 & y & -x
\end{tabularx}
\right]
\]

\end{document}

then the columns don't have the desired justification. I looked through the tabularx documentation and discovered that I should define a new column type. So I tried the following:

\documentclass{amsart}
\usepackage{amsmath}
\usepackage{array}
\usepackage{tabularx}

\begin{document}

\newcolumntype{R}{>{\raggedleft\arraybackslash}X}

\[
\left[
\begin{tabularx}{1in}{RRR}
x & 0 & y\\
0 & y & -x
\end{tabularx}
\right]
\]

\end{document}

This produced no change in output. What am I missing?

EDIT: The following code

\documentclass{amsart}
\usepackage{amsmath}
\usepackage{array}
\usepackage{tabularx}

\begin{document}

\newcolumntype{P}{>{\raggedleft\arraybackslash }p{.075in}}
\newcolumntype{Q}{>{\raggedleft\arraybackslash $}p{0.075in}<{$}}

$\left[\begin{array}{PPP}
x & 0 & y\\
0 & y & -x
\end{array}\right]$    
\qquad    
$\left[\begin{array}{QQQ}
x & 0 & y\\
0 & y & -x
\end{array}\right]$

\end{document}

produces THIS output:

enter image description here

Best Answer

First, you are missing mathmode in your example. You need \[/\] around the \left[/\right].

This produced no change in output. What am I missing?

On my machine it produces a change! The columns are right aligned.

The tabularx is not really meaningful if you use X for ALL columns. In this case just use p{<width>} columns with a normal tabular or array environment, e.g.:

  \newcolumntype{P}{>{\raggedleft\arraybackslash}p{.2in}}
  \begin{array}{PPP}

On my installation this gives almost the same result as your second code with the R column:

enter image description here

Edit: Answer to your edited problem:

The first can be fixed with \hbox{-x} and the second one with \!\!-x.

I don't think you will avoid manual adjustments here. However, with this small sizes the result will not look very nice, and LaTeX is normally about good typesetting quality.