Math & Scienceequation term alignment bypassing leading sign

Information and discussion about LaTeX's math and science related features (e.g. formulas, graphs).
Post Reply
abalenkm
Posts: 3
Joined: Sat Mar 19, 2011 2:20 pm

equation term alignment bypassing leading sign

Post by abalenkm »

Dear LaTeX community,

I would like to align the equation terms to the left inside a "{pmatrix*}[l]" environment, but at the same time bypass the leading minus sign of selected terms:

original equation:

Code: Select all

(E_1 - E_2)/\dx - (E_3 - E_4)/\dy - \Jscr
normal {pmatrix*}[l] result:

Code: Select all

(E1 - E2)/dx
- (E3 - E4)/dy
- Jsrc
desired {pmatrix*}[l] result:

Code: Select all

  (E1 - E2)/dx
 - (E3 - E4)/dy
 - Jsrc
Any help will be greatly appreciated. Thank you.
Last edited by abalenkm on Sat Mar 19, 2011 3:33 pm, edited 1 time in total.

Recommended reading 2024:

LaTeXguide.org • LaTeX-Cookbook.net • TikZ.org

NEW: TikZ book now 40% off at Amazon.com for a short time.

abalenkm
Posts: 3
Joined: Sat Mar 19, 2011 2:20 pm

equation term alignment bypassing leading sign

Post by abalenkm »

I found an answer. This is achieved with a \phantom{} command that takes the width of a given parameter, i.e. in the example above to insert a phantom minus write:

Code: Select all

\phantom{-}(E_1 - E_2)/\dx
- (E_3 - E_4)/\dy
- \Jsrc
to obtain the desired result:

Code: Select all

  (E1 - E2)/dx
- (E3 - E4)/dy
- Jsrc
You may also wish to create a custom shortcut for a \phantom command to enable a faster access to this feature:

Code: Select all

\newcommand{\p}[1]{\phantom{#1}}

\p{-}(E_1 - E_2)/\dx
- (E_3 - E_4)/\dy
- \Jsrc
Full solution example:

Code: Select all

\documentclass[a4paper,10pt]{article}

% enable advanced mathematics support
\usepackage{amsmath}

% enable pmatrix environment
\usepackage{mathtools}

% define custom mathematics commands
\newcommand{\dx}{\partial x}
\newcommand{\dy}{\partial y}
\newcommand{\Jsrc}{J_{src}}

% define \phantom shortcut
\newcommand{\p}[1]{\phantom{#1}}

\begin{document}

\[
  \begin{pmatrix*}[l]
    \p{-}(E_1 - E_2)/\dx \\
       - (E_3 - E_4)/\dy \\
       - \Jsrc
  \end{pmatrix*}
\]

\end{document}
Last edited by abalenkm on Sat Mar 19, 2011 3:45 pm, edited 1 time in total.
User avatar
localghost
Site Moderator
Posts: 9202
Joined: Fri Feb 02, 2007 12:06 pm

equation term alignment bypassing leading sign

Post by localghost »

abalenkm wrote:I found an answer. […]
Then please mark the topic (not the last post) accordingly as written in Section 3 of the Board Rules (to be read before posting). A complete code example of your solution would be desirable to make it comprehensible for others.


Best regards and welcome to the board
Thorsten
Post Reply