Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

In this question concerning negative signs and the alignment of entries in matrix environments, Heiko Oberdiek gave a great answer in which he uses a user-defined command called \matminus instead of the usual -.

How can I define a matrix environment that will automatically perform this replacement of leading negative signs for me?

Thanks for the help!

Edit:

I have adapted the code provided by Heiko Oberdiek and egreg, and created a package that defines a set of alternate matrix environments (spmatrix, sbmatrix, sBmatrix, svmatrix, and sVmatrix). These environments function analogously to their counterparts without the 's', but reduce the length of the minus signs within them to achieve better alignment of the entries within each column. The project is hosted here.

Here is a picture that shows the difference between the regular and reduced minus matrix environments:

enter image description here

share|improve this question
    
Perhaps you should consider making the columns right aligned, e.g. by using extra options to matrices provided by mathtools. –  Andrew Swann Feb 3 '13 at 18:12
    
@AndrewSwann I tried various techniques, including what you suggest, in the question that I link to above. You can see the results in the PDF file that I include with the question -- the right-aligned columns do not look very good when negative signs are used. –  void-pointer Feb 3 '13 at 20:23
add comment

1 Answer

up vote 5 down vote accepted

We can make - to become \matminus in matrices by exploiting the fact that all matrix environments of amsmath use \env@matrix, by injecting code in it.

\documentclass[11pt,a4paper]{article}
\usepackage{amsmath}
\usepackage{etoolbox}

\usepackage{graphicx}

%%% Save a copy of the original minus in math mode
\mathchardef\realminus\mathcode`-
%%% Define the shortened version of minus (H. Oberdiek)
\newcommand{\reducedminus}{%
  \leavevmode\hphantom{0}%
  \llap{%
    \settowidth{\dimen0 }{$0$}%
    \resizebox{1.1\dimen0 }{\height}{$\realminus$}%
  }%
}
%%% Define \matminus so that it doesn't reduce the minus in sub/superscripts
\newcommand{\matminus}{%
  \mathchoice{\reducedminus}{\reducedminus}%
             {\realminus}{\realminus}%
}

\makeatletter
%%% Make - become \matminus in matrices
\preto\env@matrix{\mathcode`-=\string"8000
  \begingroup\lccode`~=`-
  \lowercase{\endgroup\let~}\matminus
}
\makeatother

\begin{document}

\[
A =
\begin{pmatrix}
  -123 & -10 & 1 \\
  100 & 5 & 16 \\
  13 & 7 & 7^{-2}
\end{pmatrix}.
\]

\end{document}

enter image description here

However, I can't see any improvement over the normal appearance

enter image description here


If you want shorter minus signs also in exponents, change the code as follows:

%%% Save a copy of the original minus in math mode
\mathchardef\realminus\mathcode`-
%%% Define the shortened version of minus (H. Oberdiek)
\newcommand{\reducedminus}[2]{%
  \leavevmode\hphantom{0}%
  \llap{%
    \settowidth{\dimen0 }{$#10$}%
    \resizebox{1.1\dimen0 }{\height}{$#1\realminus$}%
  }%
}
%%% Define \matminus so that it doesn't reduce the minus in sub/superscripts
\newcommand{\matminus}{\mathpalette\reducedminus\relax}
\makeatletter
%%% Make - become \matminus in matrices
\preto\env@matrix{\mathcode`-=\string"8000
  \begingroup\lccode`~=`-
  \lowercase{\endgroup\let~}\matminus
}
\makeatother
share|improve this answer
    
Thanks for the answer. I think that the appearance of the first two columns of A has improved, but when exponents are used (as in the third column), there is no difference. –  void-pointer Feb 3 '13 at 20:24
    
@void-pointer If you want shorter minus signs also in exponents, some more work is needed. Let me add it, so you can choose. –  egreg Feb 3 '13 at 20:26
    
Thank you very much! I think the result is much better than the unmodified matrix, though the change is subtle. –  void-pointer Feb 3 '13 at 20:37
1  
@void-pointer I continue to prefer the original. –  egreg Feb 3 '13 at 20:51
    
I have adapted the code you have provided and created a package that defines a set of alternate matrix environments (spmatrix, sbmatrix, sBmatrix, svmatrix, and sVmatrix), in case others would like to experiment. The project is hosted here. –  void-pointer Feb 4 '13 at 10:45
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.