# Align environment error - missing } inserted

When I try to compile my LaTeX file, where begin{align*} environment is to be used, I constantly get an error:

! Missing } inserted.
<inserted text>
}
1.164 \end{align*}


My LaTeX snippet:

\begin{align*}
X^{(0)} = \lbrace \mathsf{PR \ R \ ST0 \ T+ \ TP \ PR \ , \ PR \ R \ ST- \ T- \ TP \ PR \ , \\ \ PR \ rs \ ST0 \ T- \ TP \ PR \ , \ PR \ rS \ ST+ \ T+ \ TP \ PR \ , \\ \ PR \ Rs \ ST+ \ T+ \ TP \ PR} \rbrace
\end{align*}


The reason I would like to use align*environment is to make the long math mode line to break. I tried to use different commands (linebreak, allowbreak, //, etc.), but they would not help.

• Welcome to TeX.SX! I guess the problem is that you open \mathsf in the first line and then don't close it with } before the end of the line marked by \\ . If you want to keep this approach of \mathsfying everything at once, you have to repeat it in every line of align*. – gernot Sep 18 '16 at 11:14
• @gernot the problem lies with the linebreaking inside the align*. all braces are present and accounted for. – naphaneal Sep 18 '16 at 12:42
• @naphaneal \mathsf{ ... \\ ...} causes the problem. If you want to do a general switch to mathsf (probably not, but if) then you have to repeat it, like \mathsf{ ... } \\ \mathsf{ ...}. – gernot Sep 18 '16 at 12:56
• Could you give details of what does that represent? Just to understand what exactly are you writing about, and may be suggest better ways. – Manuel Sep 18 '16 at 13:46
• Hi, these are "structural primitives" representing ECG signal elements. – Piotr Flasiński Sep 19 '16 at 12:54

In my comment above I have already pointed out what causes the error message. Beyond that, I'm not convinced that you are on the right track. Pure guessing, but I suppose that each of these letter combinations designates some entity that will occur more frequently. To achieve consistent typesetting I suggest to use macros. E.g., you can rewrite your example as follows.

\documentclass{article}
\usepackage{amsmath}
\newcommand\PR{\mathsf{PR}}
\newcommand\R{\mathsf{R}}
\newcommand\STz{\mathsf{ST_0}}
\newcommand\STm{\mathsf{ST_-}}
\newcommand\STp{\mathsf{ST_+}}
\newcommand\Tp{\mathsf{T_+}}
\newcommand\Tm{\mathsf{T_-}}
\newcommand\TP{\mathsf{TP}}
\newcommand\rs{\mathsf{rs}}
\newcommand\rS{\mathsf{rS}}
\newcommand\Rs{\mathsf{Rs}}
\begin{document}
\begin{align*}
X^{(0)} = \lbrace\;
& \PR\;\R\;\STz\;\Tp\;\TP\;\PR,\;
\PR\;\R\;\STm\;\Tm\;\TP\;\PR, \\
& \PR\;\rs\;\STz\;\Tm\;\TP\;\PR,\;
\PR\;\rS\;\STp\;\Tp\;\TP\;\PR, \\
& \PR\;\Rs\;\STp\;\Tp\;\TP\;\PR\;\rbrace
\end{align*}
\end{document}


This will give the following output.

To avoid to hard-code the formatting of the chains of identifiers (like putting some space inbetween), you can put the definitions

\newcommand\chainend{\chainend}
\newcommand\chain[1]{\chainx#1\chainend}
\newcommand\chainx[1]%
{\ifx\chainend#1\let\chainy\unskip % at the end, remove the last \;
\else#1\;\let\chainy\chainx % here \; is inserted between any two items of the chain
\fi
\chainy
}
\newcommand\chainy{}


into the preamble and then just write

 \chain{\PR\R\STz\Tp\TP\PR}


 \PR\;\R\;\STz\;\Tp\;\TP\;\PR


The problem is that you can't end a line when \mathsf has not yet ended.

Decide where the breaks should go and add \mathsf:

\documentclass{article}
\usepackage{amsmath}

\begin{document}

\begin{equation*}
\begin{aligned}
X^{(0)} = \lbrace
& \mathsf{PR \ R \ ST0 \ T{+} \ TP \ PR, \ PR \ R \ ST{-} \ T{-} \ TP \ PR,} \\
& \mathsf{PR \ rs \ ST0 \ T{-} \ TP \ PR, \ PR \ rS \ ST{+} \ T{+} \ TP \ PR,} \\
& \mathsf{PR \ Rs \ ST{+} \ T{+} \ TP \ PR} \rbrace
\end{aligned}
\end{equation*}

\end{document}


Note also {+} and {-} in order not to get bad spacing. I believe it's conceptually preferable to use aligned rather than align*. Also I wouldn't space the commas.

A more complicated setting, but ending in simpler input, with the same output.

\documentclass{article}
\usepackage{amsmath}

\newcommand{\entity}[1]{%
\mathsf{%
\catcode\ =12
\begingroup\lccode~=\ \lowercase{\endgroup\def~{\mathclose{\ }}}%
\scantokens{#1\empty}%
}%
}

\begin{document}

\begin{equation*}
\begin{aligned}
X^{(0)} = \lbrace
& \entity{PR R ST0 T+ TP PR}, \entity{PR R ST- T- TP PR}, \\
& \entity{PR rs ST0 T{-} TP PR}, \entity{PR rS ST{+} T{+} TP PR}, \\
& \entity{PR Rs ST{+} T{+} TP PR} \rbrace
\end{aligned}
\end{equation*}

\end{document}


With less tricky code: we split the input at spaces, then reinsert \mathclose{\ } at their place; different spacings can be obtained by changing the argument to \mathclose.

\documentclass{article}
\usepackage{amsmath}
\usepackage{xparse}

\ExplSyntaxOn
\NewDocumentCommand{\entity}{m}
{
\seq_set_split:Nnn \l_piotr_entity_seq { ~ } { #1 } %split at spaces
\mathsf
{
\seq_use:Nn \l_piotr_entity_seq { \mathclose{\ } }
}
}
\ExplSyntaxOff

\begin{document}

\begin{equation*}
\begin{aligned}
X^{(0)} = \lbrace
& \entity{PR R ST0 T+ TP PR}, \entity{PR R ST- T- TP PR}, \\
& \entity{PR rs ST0 T{-} TP PR}, \entity{PR rS ST{+} T{+} TP PR}, \\
& \entity{PR Rs ST{+} T{+} TP PR} \rbrace
\end{aligned}
\end{equation*}

\end{document}
`