3

I'm using amsmath and want to display a state-space representation where all equal-signs are alligned, and the elements in-between are centered. Just like align should do, to my understanding. But I get this, where all blocks are aligned right. Not to mention weird blank spaces inserted after each =: enter image description here

Code:

\documentclass{article}
\usepackage{amsmath}
\renewcommand{\vec}{\textbf}
\begin{document}
    \begin{align}
    \dot{\vec{x}} &=& \begin{bmatrix}
    \dot{n}\\\dot{x}\\\dot{x_c}\\\dot{V_v}\\\dot{V_m}\\\dot{H}
    \end{bmatrix} &=& \begin{bmatrix}
    f_2(f_1(I),n)\\
    f_4(x, x_c, f_3(f_1(I),n,x,x_c))\\
    f_5(x, x_c, f_3(f_1(I),n,x,x_c))\\
    f_8(f_7(f_6(x, x_c),H), V_v, f_9(V_m))\\
    f_{10}(V_m, f_{11}(V_v))\\
    f_{12}(H, f_9(V_m))
    \end{bmatrix} &=& \vec{f}(\vec{x}, \vec{u}; \theta)
    \label{eq:state}\text{, and}\\
    \vec{z} &=& \begin{bmatrix}
    S_w
    \end{bmatrix} &=& \begin{bmatrix}
    h_1(f_9(V_m))
    \end{bmatrix} &=& \vec{h}(\vec{x}; \theta)
    \label{eq:output}\text{.}
    \end{align}
\end{document}
JHBonarius
  • 357
  • 3
  • 10
  • Off-topic: Use \mathbf, not \textbf, for math-mode material. – Mico Mar 12 '18 at 15:27
  • just one comment. placing & signs after = signs is eqnarray notation, not any of the amsmath alignment structures. that's the reason for the extra space. – barbara beeton Mar 12 '18 at 16:29

2 Answers2

3

That is the standard behaviour for align, since it inserts gaps (horizontally) depending on the use of &. There is an assumption that multiple equations are to be spread out. And your current use of &=& around the relations resembles what was required when using eqnarray. This is not the case with amsmath's align-and-friends.

To force centre alignment, you can store the content in a box and use measurements to push smaller boxes into the right position. eqparbox provides this out of the box with \eqmakebox[<tag>][<align>]{<stuff>}, <align>ing (left, centre/default, right) all <stuff> with the same <tag> as required.

enter image description here

\documentclass{article}

\usepackage{amsmath,bm}
\usepackage{eqparbox,xparse}

% https://tex.stackexchange.com/a/34412/5764
\makeatletter
\NewDocumentCommand{\eqmathbox}{o O{c} m}{%
  \IfValueTF{#1}
    {\def\eqmathbox@##1##2{\eqmakebox[#1][#2]{$##1##2$}}}
    {\def\eqmathbox@##1##2{\eqmakebox{$##1##2$}}}
  \mathpalette\eqmathbox@{#3}
}
\renewcommand{\vec}{\bm}

\begin{document}

\begin{align}
  \dot{\vec{x}} &= \eqmathbox[left]{\begin{bmatrix}
    \dot{n} \\ \dot{x} \\ \dot{x_c} \\ \dot{V_v} \\ \dot{V_m} \\ \dot{H}
  \end{bmatrix}} = \eqmathbox[middle]{\begin{bmatrix}
    f_2( f_1( I ), n) \\
    f_4( x, x_c, f_3( f_1( I ), n, x, x_c ) ) \\
    f_5( x, x_c, f_3( f_1( I ), n, x, x_c ) ) \\
    f_8( f_7( f_6( x, x_c ), H ), V_v, f_9( V_m ) ) \\
    f_{10}( V_m, f_{11}( V_v ) ) \\
    f_{12}( H, f_9( V_m ) )
  \end{bmatrix}} = \eqmathbox[right]{\vec{f}( \vec{x}, \vec{u}; \theta ),}
  \label{eq:state} \text{ and} \\
  \vec{z} &= \eqmathbox[left]{\begin{bmatrix}
    S_w
  \end{bmatrix}} = \eqmathbox[middle]{\begin{bmatrix}
    h_1( f_9( V_m ) )
  \end{bmatrix}} = \eqmathbox[right]{\vec{h}( \vec{x}; \theta ).}
  \label{eq:output}
\end{align}

\end{document}

This requires at least two compilations with every change in content within an \eqmathbox.

Werner
  • 603,163
  • This looks like what I want. But it seems quite a hassle, especially the two compilations... No easier way? – JHBonarius Mar 12 '18 at 18:36
  • 1
    @JHBonarius: Two compilations are only required when there is a change in <stuff> fot the same <tag>. Once <stuff> is settled/finalized, you only have to compile once. There are alternatives, like placing the largest content in a box that you can use for measurement in the placing of the smaller components. It would require less compilation, but clutter your code. eqparbox automates this approach with the addition of compilation (again, only when <stuff> changes for the same <tag>). If you're not interested in equation numbering, an array would work. – Werner Mar 12 '18 at 18:41
2

I don't think that providing multipled alignment points across the two equations really helps elucidate what the two equations "mean". Why not use a gather environment, to create two individually-centered equations.

enter image description here

\documentclass{article}
\usepackage{mathtools} % for '\shortintertext' macro
\renewcommand{\vec}{\mathbf} % use '\mathbf', not '\textbf' 
\begin{document}
    \begin{gather}
    \dot{\vec{x}} =
    \begin{bmatrix}
      \dot{n}\\ \dot{x}\\ \dot{x_c}\\ \dot{V_v}\\ \dot{V_m}\\ \dot{H}
    \end{bmatrix} =
    \begin{bmatrix}
      f_2(f_1(I),n)\\
      f_4(x, x_c, f_3(f_1(I),n,x,x_c))\\
      f_5(x, x_c, f_3(f_1(I),n,x,x_c))\\
      f_8(f_7(f_6(x, x_c),H), V_v, f_9(V_m))\\
      f_{10}(V_m, f_{11}(V_v))\\
      f_{12}(H, f_9(V_m))
    \end{bmatrix} =
    \vec{f}(\vec{x}, \vec{u}; \theta)
    \label{eq:state}\\
    \shortintertext{and}
    \vec{z} =
    \begin{bmatrix}
      S_w
    \end{bmatrix} =
    \begin{bmatrix}
      h_1(f_9(V_m))
    \end{bmatrix} =
    \vec{h}(\vec{x}; \theta).
    \label{eq:output}
    \end{gather}
\end{document}
Mico
  • 506,678
  • I had it like this (somewhat). But I need to shrink my 8 page article to a 4-page limit, so want to save all the space I have. The state-space representation equations are usually written below each other. – JHBonarius Mar 12 '18 at 18:38
  • @JHBonarius - I suppose a tiny bit of vertical space could be saved by moving the word "and" to the end of the preceding line, rather than placing it on a line by itself. – Mico Mar 12 '18 at 19:26