4

I have a problem creating an equation, where I first want to write it as text and then resolve it to numbers.

This is a minimal example:

\documentclass[headings=small,12pt,a4paper]{scrartcl}
\usepackage[utf8]{inputenc}
\usepackage{graphicx}
\usepackage{float}
\usepackage{amsmath}

\newcommand{\mtxt}[1]{\textsc{#1}}

\begin{document}
\begin{equation}
\begin{aligned}
\mtxt{Total Size} & =\mtxt{Text1} & + \mtxt{Text2} & + \mtxt{Text3} & + \mtxt{Text4} & + \mtxt{Text5}\\
& = 1 & + 2 & + 3 & + 4 & + 5
\end{aligned}
\end{equation} 
\end{document}

Problem is, that the first alignment is correct, the third one is too, but the other ones aren't.

result image

This is the result and clearly not what I wished for, but I diving into many aligned articles I still cannot grasp why this is happening.

Edit: Expected is that indentation aligns after each &, so the plus signs align with enough space for each cell in the column there. Like seen in the picture Christian Hupfer and also the one by Steven B. Segletes.

  • 2
    Welcome to TeX.SX! You should tell us what you expect! – user31729 Oct 15 '15 at 13:14
  • 2
    the example isn't what the code produces. for one thing, the code specifies \textsc and the example shows ordinary lowercase. – barbara beeton Oct 15 '15 at 14:45
  • You are right. thats because I shortly switched the mtxt command to \text to try it out if that was the reason, but it wasnt. But thats why I did the command, to fast switch it :) – Crumar Oct 15 '15 at 14:47
8

You should bear in mind that aligned makes pairs of columns, one right aligned and one left aligned, adding space between each pair.

You want alignat that doesn't add space between pairs of columns, but jumping over the right aligned columns.

\documentclass[headings=small,12pt,a4paper]{scrartcl}
\usepackage[utf8]{inputenc}
\usepackage{graphicx}
\usepackage{float}
\usepackage{amsmath}

\newcommand{\mtxt}[1]{\textsc{#1}}

\begin{document}
\begin{equation}
\begin{alignedat}{5}
\mtxt{Total Size} & =\mtxt{Text1} && + \mtxt{Text2} && + \mtxt{Text3} && + \mtxt{Text4} && + \mtxt{Text5}\\
& = 1 && + 2 && + 3 && + 4 && + 5
\end{alignedat}
\end{equation}
\end{document}

enter image description here

  • I accepted this because it works, looks more clean and because you explained what is happening, though Christian Hupfer's answer is also correct and working – Crumar Oct 15 '15 at 14:21
4

Unclear what is requested, so this is a trial with alignat:

(Note: The spacing between + and the Text has to be changed)

\documentclass[headings=small,12pt,a4paper]{scrartcl}
\usepackage[utf8]{inputenc}
\usepackage{graphicx}
\usepackage{float}
\usepackage{amsmath}

\newcommand{\mtxt}[1]{\text{\textsc{#1}}}

\begin{document}
\begin{alignat}{5}
\mtxt{Total Size} & =\mtxt{Text1} & +& \mtxt{Text2} & +& \mtxt{Text3} & +& \mtxt{Text4} & +& \mtxt{Text5}\\
& = 1 & +& 2 & +& 3 & +& 4 & +& 5
\end{alignat}
\end{document}

enter image description here

  • It is working, but it increments the equation counter each line. Is there a way to work around that? – Crumar Oct 15 '15 at 13:31
  • @Crumar: Either use alignedat within equation as egreg suggested or say \nonumber after the number '5' in your second line. – user31729 Oct 15 '15 at 13:35
  • 2
    the spacing around the plus signs is too tight. you could fix that by inputting them as {}+{} but really, doubling the & just before each plus is easier. – barbara beeton Oct 15 '15 at 14:42
  • 1
    @barbarabeeton: Thanks (as usual!), but I've wrote already in my post: The spacing must be changed -- I leave this to the O.P. – user31729 Oct 15 '15 at 15:56
1

Done here with two varieties of TABstack, namely a \tabbedCenterstack with left alignment, and a \tabularCenterstack. Note that proper spacing around the + and = signs have been achieved.

\documentclass[headings=small,12pt,a4paper]{scrartcl}
\usepackage[utf8]{inputenc}
\usepackage{graphicx}
\usepackage{float}
\usepackage{amsmath}
\usepackage{tabstackengine}
\stackMath
\newcommand{\mtxt}[1]{\textsc{#1}}
\begin{document}
\begin{equation}
\TABbinaryLeft
\tabbedCenterstack[l]{
  \mtxt{Total Size} & =\mtxt{Text1} & + \mtxt{Text2} & + \mtxt{Text3} & 
  + \mtxt{Text4} & + \mtxt{Text5}\\
& = 1 & + 2 & + 3 & + 4 & + 5
}
\end{equation} 
\begin{equation}
\TABbinary\setstacktabulargap{0pt}
\tabularCenterstack{rclclclclc}{
  \mtxt{Total Size} =& \mtxt{Text1} & +& \mtxt{Text2} & +& \mtxt{Text3} & 
  +& \mtxt{Text4} & +& \mtxt{Text5}\\
=&  1 & +& 2 & + &3 & + &4 & +& 5
}
\end{equation} 
\end{document}

enter image description here

By way of explanation, a \TABbinaryLeft places a {} at the beginning of every field, to activate the binary nature of fields starting with a + or =. A \TABbinary places a {} at the beginning and end of every field, for fields beginning in a + and ending in a =, for example. The default package behavior is \TABbinaryRight, which is why the default tab character placement should occur (by default) after operators.

Also, the \setstacktabulargap{0pt} setting removes the default \tabcolsep separation between adjacent columns of a \tabularCenterstack, which is desired for setting maths.

  • I want to try this, even though it looks a lot like too much effort for me. Unfortunately, the effort even increases. On the CTAN page of the package (which as I saw you are maintaining), it say it is included in MikTex, but the package tabstackengine is not found. MikTex doesnt offer to download it, and a search in the package manager did also not yield results. As I tend to switch between machines while writing, it is a lot of effort to maintan all packages everywhere :/ – Crumar Oct 15 '15 at 13:35
  • @Crumar It is there, at ctan.org/pkg/tabstackengine. I don't know why MikTeX won't download it (maybe your installation is old). But you can manually place the .sty in your working directory for now. Default installation with MikTeX would be in C:\Users\<your-username>\AppData\Roaming\MiKTeX\2.9\tex\latex\tabstackengine – Steven B. Segletes Oct 15 '15 at 13:36

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.