2

I'm doing something wrong in the code below that makes the last equation not be written on a line by itself. What I'm trying to do is to split the penultimate equation into two lines. But this happens:

enter image description here

The last equation continues on the right for some reason. Can you spot what is the problem?

\usepackage{amsmath}
\newcommand{\q}[2]{\langle #1 | #2 \rangle}
\newcommand{\qr}[1]{|#1\rangle} 
\newcommand{\ql}[1]{\langle #1|} 
\DeclareMathOperator{\mdc}{mdc}

\begin{document}
\begin{align*}
  \qr{\psi_2} &= U_f \qr{\psi_1}\\
              &= U_f \frac{1}{\sqrt{2^4}} \left[\qr0 + \qr1 + \qr2+\qr3+\qr4+\qr5+\qr6 + \qr{7}\right] \qr0 \qr0\\
              &= U_f \frac{1}{\sqrt{2^4}} \left[\qr0\qr0 + \qr1\qr0 + \qr2\qr0+\qr3\qr0+\qr4\qr0+\qr5\qr0+\qr6\qr0 + \qr{7}\qr0\right] \qr0\\
\begin{split}
              &= \frac{1}{\sqrt{2^4}} \big[U_f\qr0\qr0 + U_f\qr1\qr0 + U_f\qr2\qr0+U_f\qr3\qr0 \\
              &\qquad\qquad + U_f\qr4\qr0+U_f\qr5\qr0+U_f\qr6\qr0 + U_f\qr{7}\qr0\big] \qr0\\
\end{split}
              &= \frac{1}{\sqrt{2^4}} \left[\qr0\qr2 + \qr1\qr4 + ... + \qr{i}\qr{f(i)} + ... + \qr{7}\qr1\right] \qr0,
\end{align*}

\end{document}

EDIT: I must've been very tired when I wrote this question because, if you know a little quantum computing, you can see I should have 16 states and not 8 as I wrote.

  • 2
    I'm not quite sure what you're aiming for, but have you tried commenting out the \begin{split} and \end{split}, and leaving the inner two lines alone? – Teepeemm Feb 22 at 19:26
  • Wow. Why am I using split in the first place? I thought this wouldn't work at all. – Joep Awinita Feb 22 at 19:28
  • Needless to say, @Teepeemm, you're answered the question completely. – Joep Awinita Feb 22 at 19:36
  • 1
    But a two line comment takes far less effort than a complete answer. I'm content with letting those wanting to take the time to also take the points. – Teepeemm Feb 22 at 20:29
  • @Mico added the very interesting suggestion of U_{\!f} in lieu of U_f. I had not even detected the cosmetic problem of the subscript being a little far from its main symbol, so I'll choose his answer. Thanks! – Joep Awinita Feb 23 at 13:38
4

In addition to getting rid of the unneeded split environment, you may want to (a) typeset the U_f terms more compactly, i.e., as U_{\!f}, (b) replace various \left[ and \right] directives with \bigl[ and \bigr], and (c) replace ... with \dots.

enter image description here

\documentclass{article}
\usepackage{mathtools} % for '\DeclarePairedDelimiter' macro
\DeclarePairedDelimiter\qr\lvert\rangle
\newcommand{\Uf}{U_{\!f}}

\begin{document}

\begin{align*}
\qr{\psi_2} 
&= \Uf \qr{\psi_1}\\
&= \Uf \frac{1}{\sqrt{2^4}} 
   \bigl[\qr0 + \qr1 + \qr2+\qr3 +\qr4 
   +\qr5 +\qr6 + \qr7\bigr] \qr0 \qr0\\
&= \Uf \frac{1}{\sqrt{2^4}} 
   \bigl[\qr0\qr0 + \qr1\qr0 + \qr2\qr0+\qr3\qr0
   +\qr4\qr0+\qr5\qr0+\qr6\qr0 + \qr7\qr0\bigr] \qr0\\
&= \frac{1}{\sqrt{2^4}} 
   \bigl[\Uf\qr0\qr0 + \Uf\qr1\qr0 + \Uf\qr2\qr0+\Uf\qr3\qr0 \\
&\qquad\qquad 
   + \Uf\qr4\qr0+\Uf\qr5\qr0+\Uf\qr6\qr0 
   + \Uf\qr7\qr0\bigr] \qr0\\
&= \frac{1}{\sqrt{2^4}} 
   \bigl[\qr0\qr2 + \qr1\qr4 + \dots 
   + \qr{i}\qr{f(i)} + \dots + \qr7\qr1\bigr] \qr0,
\end{align*}
\end{document}
  • 1
    Very interesting suggestions. (Thank you!) – Joep Awinita Feb 23 at 13:35
1

I suggest using the multlined environment from mathtools, and the \DeclarePairedDelimiter command from the same package, which will produce scaling \, \qr and \ql:

\documentclass{article}
\usepackage{mathtools}
\DeclarePairedDelimiterX\norm[1]\lVert\rVert{\ifblank{#1}{\: · \:}{#1}}
\DeclarePairedDelimiterXPP{\twonorm }[1]{}{\lVert}{\rVert}{_{2}}{\ifblank{#1}{\: · \:}{#1}}
\DeclarePairedDelimiterX\q[2]{\langle}{\rangle}%
{#1\,\delimsize\vert\,\mathopen{}#2}
\DeclarePairedDelimiter{\qr}\vert\rangle
\DeclarePairedDelimiter{\ql}\langle \rvert
\DeclareMathOperator{\mdc}{mdc}

\begin{document}

\begin{align*}
  \qr{\psi_2} &= U_f \qr{\psi_1}\\
              &= U_f \frac{1}{\sqrt{2^4}} \bigl[\qr0 + \qr1 + \qr2+\qr3+\qr4+\qr5+\qr6 + \qr{7}\bigr] \qr0 \qr0\\
              &= U_f \frac{1}{\sqrt{2^4}}\bigl[\qr0\qr0 + \qr1\qr0 + \qr2\qr0+\qr3\qr0+\qr4\qr0+\qr5\qr0+\qr6\qr0 + \qr{7}\qr0\bigr] \qr0\\
              &= \begin{multlined}[t] \frac{1}{\sqrt{2^4}} \bigl[U_f\qr0\qr0 + U_f\qr1\qr0 + U_f\qr2\qr0+U_f\qr3\qr0 \\[-1.5ex]
             \qquad+ U_f\qr4\qr0+U_f\qr5\qr0+U_f\qr6\qr0 + U_f\qr{7}\qr0\bigr] \qr0
\end{multlined}\\
              &= \frac{1}{\sqrt{2^4}}\bigl[\qr0\qr2 + \qr1\qr4 + ... + \qr{i}\qr{f(i)} + ... + \qr{7}\qr1\bigr] \qr0,
\end{align*}

\end{document} 

enter image description here

  • The \left and \right directives don't succeed in making "big" square brackets -- better to use \bigl[ and \bigr], right? :-) – Mico Feb 22 at 20:36
  • That's right. Actually, I didn't think about this problem, but I see I should have, to make the formula a little less unpleasant to read. I'll fix that. Thanks! – Bernard Feb 22 at 20:48
1

You can use aligned instead of split, but also split could do provided you issue \\ at the right spot.

Instead of making your own bras and kets, it's better if you use a specialized package such as braket. I also removed all useless \left and \right, which don't do anything beyond adding unnecessary space, using instead \bigl and \bigr (not \big).

\documentclass{article}
\usepackage{amsmath}
\usepackage{braket}

\newcommand{\q}[2]{\braket{#1|#2}}
\newcommand{\qr}[1]{\ket{#1}}
\newcommand{\ql}[1]{\bra{#1}}

\DeclareMathOperator{\mdc}{mdc}

\begin{document}

\begin{align*}
\qr{\psi_2}
&= U_f \qr{\psi_1}\\
&= U_f \frac{1}{\sqrt{2^4}}
   \bigl[\qr0 + \qr1 + \qr2 + \qr3 + \qr4 + \qr5 + \qr6 + \qr{7}\bigr] \qr0 \qr0 \\
&= U_f \frac{1}{\sqrt{2^4}}
   \bigl[
     \qr0\qr0 + \qr1\qr0 + \qr2\qr0 + \qr3\qr0 + \qr4\qr0 +
     \qr5\qr0 + \qr6\qr0 + \qr{7}\qr0
   \bigr] \qr0 \\
&= \begin{aligned}[t]
   \frac{1}{\sqrt{2^4}}
   \bigl[
    &U_f\qr0\qr0 + U_f\qr1\qr0 + U_f\qr2\qr0+U_f\qr3\qr0 \\
    &\quad+ U_f\qr4\qr0+U_f\qr5\qr0+U_f\qr6\qr0 + U_f\qr{7}\qr0
   \bigr] \qr0
\end{aligned}\\
&= \frac{1}{\sqrt{2^4}}
   \bigl[
     \qr0\qr2 + \qr1\qr4 + \dots + \qr{i}\qr{f(i)} + \dots + \qr{7}\qr1
   \bigr] \qr0,
\end{align*}

\end{document}

enter image description here

You get essentially the same with split:

\documentclass{article}
\usepackage{amsmath}
\usepackage{braket}
\newcommand{\q}[2]{\braket{#1|#2}}
\newcommand{\qr}[1]{\ket{#1}}
\newcommand{\ql}[1]{\bra{#1}}

\DeclareMathOperator{\mdc}{mdc}

\begin{document}

\begin{align*}
\qr{\psi_2}
&= U_f \qr{\psi_1}\\
&= U_f \frac{1}{\sqrt{2^4}}
   \bigl[\qr0 + \qr1 + \qr2 + \qr3 + \qr4 + \qr5 + \qr6 + \qr{7}\bigr] \qr0 \qr0 \\
&= U_f \frac{1}{\sqrt{2^4}}
   \bigl[
     \qr0\qr0 + \qr1\qr0 + \qr2\qr0 + \qr3\qr0 + \qr4\qr0 +
     \qr5\qr0 + \qr6\qr0 + \qr{7}\qr0
   \bigr] \qr0 \\
\begin{split}
&=\frac{1}{\sqrt{2^4}}
   \bigl[
    U_f\qr0\qr0 + U_f\qr1\qr0 + U_f\qr2\qr0+U_f\qr3\qr0 \\
    &\qquad\qquad+ U_f\qr4\qr0+U_f\qr5\qr0+U_f\qr6\qr0 + U_f\qr{7}\qr0
   \bigr] \qr0
\end{split}\\
&= \frac{1}{\sqrt{2^4}}
   \bigl[
     \qr0\qr2 + \qr1\qr4 + \dots + \qr{i}\qr{f(i)} + \dots + \qr{7}\qr1
   \bigr] \qr0,
\end{align*}

\end{document}
0

I think the reason you are getting this result is that the second linebreak inside the split environment should be outside, like this:

\begin{align*}
  \qr{\psi_2} &= U_f \qr{\psi_1}\\
              &= U_f \frac{1}{\sqrt{2^4}} \left[\qr0 + \qr1 + \qr2+\qr3+\qr4+\qr5+\qr6 + \qr{7}\right] \qr0 \qr0\\
              &= U_f \frac{1}{\sqrt{2^4}} \left[\qr0\qr0 + \qr1\qr0 + \qr2\qr0+\qr3\qr0+\qr4\qr0+\qr5\qr0+\qr6\qr0 + \qr{7}\qr0\right] \qr0\\
\begin{split}
              &= \frac{1}{\sqrt{2^4}} \big[U_f\qr0\qr0 + U_f\qr1\qr0 + U_f\qr2\qr0+U_f\qr3\qr0 \\
              &\qquad\qquad + U_f\qr4\qr0+U_f\qr5\qr0+U_f\qr6\qr0 + U_f\qr{7}\qr0\big] \qr0
\end{split}\\
              &= \frac{1}{\sqrt{2^4}} \left[\qr0\qr2 + \qr1\qr4 + ... + \qr{i}\qr{f(i)} + ... + \qr{7}\qr1\right] \qr0,
\end{align*}

Besides the recomendations given in Bernard's answer, I'd also suggest using the split environment inside equation* instead of align*, since it seems you're trying to typeset just one (albeit multi-line) equation:

\documentclass{article}
\usepackage{amsmath}
\newcommand{\q}[2]{\langle #1 | #2 \rangle}
\newcommand{\qr}[1]{|#1\rangle} 
\newcommand{\ql}[1]{\langle #1|} 
\DeclareMathOperator{\mdc}{mdc}

\begin{document}

\begin{equation*}
 \begin{split}
  \qr{\psi_2} &= U_f \qr{\psi_1}\\
              &= U_f \frac{1}{\sqrt{2^4}} \left[\qr0 + \qr1 + \qr2+\qr3+\qr4+\qr5+\qr6 + \qr{7}\right] \qr0 \qr0\\
              &= U_f \frac{1}{\sqrt{2^4}} \left[\qr0\qr0 + \qr1\qr0 + \qr2\qr0+\qr3\qr0+\qr4\qr0+\qr5\qr0+\qr6\qr0 + \qr{7}\qr0\right] \qr0\\
              &=\!\begin{multlined}[t] \frac{1}{\sqrt{2^4}} \big[U_f\qr0\qr0 + U_f\qr1\qr0 + U_f\qr2\qr0+U_f\qr3\qr0 \\[-1.5ex]
             \qquad+ U_f\qr4\qr0+U_f\qr5\qr0+U_f\qr6\qr0 + U_f\qr{7}\qr0\big] \qr0
\end{multlined}\\
              &= \frac{1}{\sqrt{2^4}} \left[\qr0\qr2 + \qr1\qr4 + ... + \qr{i}\qr{f(i)} + ... + \qr{7}\qr1\right] \qr0,
 \end{split}
\end{equation*}

\end{document} 
  • 1
    But how to align the equal signs within equation? align is appropriate here. – Sigur Feb 22 at 20:20
  • I forgot to add that I'd recomend using the split environment to align the equal signs and the multlined environment to break one the lines into two. – Vinicius M. G. Silveira Feb 22 at 20:24

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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