# Adding a complex maths equation to Latex

I am trying to add the following equation to my paper.

This is my code and it is not producing desired output.

$$L_T(\vec{\lambda}) = \sum_(x,s)\elementof T \log P(s|X) - \sum_(i=1)^m \lamda_i^$$
\subsection{Partition Function}


Any simple way to create this equation?

• Add \textstyle just after and write your code as \mathcal{L}_{\mathcal{T}}(\vec{\lambda}) = \sum_{\mathbf{x},\mathbf{s}\in T} \log P(\mathbf{x}|\mathbf{S}) - \sum_{i=1}^m \frac{\lambda_i^2}{2\sigma^2}. But you should define a semantic command rather than use \mathbf, and also I don't see a reason to use \textstyle there, it looks better by default (which would equal \displaystyle). – Manuel Sep 27 '16 at 11:54
• Why I am not getting that calligraphic $L$ – karu Sep 27 '16 at 11:56
• @Manuel Could you please post it as an answer and explain it ? – karu Sep 27 '16 at 11:59

I kept to the OPs image rather than to his code to keep the result as required. an MWE could be

\documentclass{scrartcl}
\usepackage[utf8]{inputenc}
\usepackage{amsmath,amssymb,amsthm}
\begin{document}
$\mathcal L_{\mathcal T}(\vec{\lambda}) = \sum_{(\mathbf{x},\mathbf{s})\in \mathcal T} \log P(\mathbf{s}\mid\mathbf{x}) - \sum_{i=1}^m \frac{\lambda_i^2}{2\sigma^2}$
\end{document}


In there, \mathcal defines the next letter to be caligraphic, \mathbfa bold face one. You should introduce semantic commands (for example I use bold for vectors and hence define \vectin mit documents. This defines globally how certain things should look like and makes the formulae semantically nicer.

However the result looks like

• Add \textstyle because otherwise the index of the sum is under the sum symbol. – CarLaTeX Sep 27 '16 at 12:11
• You're right. I like it indeed more that way but it does not fit the OPs request. Manuel did it right, so my answer might stay as it is. – Ronny Sep 27 '16 at 12:14

This doubts have to do with basic of how TeX (and LaTeX) operates, you should read an introduction.

Now, to your question, this will recreate exactly that output

\documentclass{article}
\begin{document}
$$\textstyle \mathcal{L}_{\mathcal{T}}(\vec{\lambda}) = \sum_{\mathbf{x},\mathbf{s}\in\mathcal{T}} \log P(\mathbf{x}|\mathbf{S}) - \sum_{i=1}^m \frac{\lambda_i^2}{2\sigma^2}$$
\end{document}


But there's no reason for that \textstyle to be there, in centered equations super- or subscripts are usually placed above and below the sumation symbol. So better to leave it out (unless you specifically want to have that exact output).

In any case, I would define semantic commands for \mathcal, \mathbf, and even may be \mathcal{L} or \mathcal{T} or P. A few examples

• If in one document one wants the vectors to be written with bold fonts, rather than an arrow above (which is not the case in your code, but it's just an example) it's better to use \vec{x} (rather than \mathbf{x}) and define \renewcommand*\vec[1]{\mathbf{#1}}.

• If you want to use \mathbb{N} to denote number sets, may be it's better to use \numberset{N} or \numberset{K} in your document, rather than \mathbb{..}. That way you have a semantic macro which also gives you freedom to redefine the macro whenever you want;

• even more, you can, in turn, define \N or \naturals to be \numberset{N}, or \reals/\R to be \numberset{R}. That way you abstract even further. The document would be simpler to write and to read.

• And you can use \Pr for the \Pr(x|y) in case it's a probability, and may be redefine \Pr to your taste.

Of course, if you are using something once in a document, there's no need for this, but if you are using something again and again, it's definitely beneficial for your document to have semantic and symbolic macros, at a more higher level, rather than use the “core tools” directly.

• good answer, except for the recommendation to define one-letter commands (\N and \R). i think those letters aren't actually in use at the moment, but many other single letters (and almost all single lowercase letters) are, and defining additional ones gets one into bad habits, especially someone who is not already quite familiar with the system. also, regarding \vec, in this case, a new command \bvec could be appropriate, allowing both forms to be used freely in the same document. – barbara beeton Sep 27 '16 at 12:33
• I only use single letter commands for those things, that I think are pretty understandable (\N\Q\R\I\Z\C), but yes, I wouldn't recommend that single letter macros in general. About \bvec yes, that's up to @karu, I was pointing that it was definitely not a \vec since \vec was in use. May be it was another mathematical “entity”. If it's just a vector that by convention is written in bold rather than an arrow, a variant \bvec is perfect. – Manuel Sep 27 '16 at 12:40
• You should \mid instead of | to denote conditioning. – Mico Sep 27 '16 at 12:46
• @Mico I think \mid is too much space. I tend to define \given and use \Pr(x\given y) and usually, define \given as \,|\, (more or less). But here I think | even looks better than \mid. – Manuel Sep 27 '16 at 12:48
• In the TeXbook, Knuth suggests writing (\,\mathbf{x}\mid\mathbf{S}\,) -- thinspaces inside the opening and closing parentheses and thickspace around the | symbol. – Mico Sep 27 '16 at 12:51

Please note (mandatory) arguments to a LaTeX command have to be enclosed within braces, not parentheses.

I propose some improvements: using \mid or \,\vert\, instead of | for a better spacing. Similarly, \smashoperator (from mathtools) sets the problem of subscripts or superscripts in \sum which are larger than the sum symbol and result in wide spacings in equations. Finally I recommend using \vv from \esvect, rather than vect: it produces nicer arrow tips (8 variants!) and are extensible. Compare:

\documentclass{article}
\usepackage{mathtools}
\usepackage{array}
\usepackage[b]{esvect}

\begin{document}

$$\mathcal{L}_T(\vv{λ}) = \smashoperator{∑_{\mathbf{x},\mathbf{s} ∈ \mathcal{T}}} \log P(\mathbf{s}\,\vert\, \mathbf{x}) - ∑_{i=1}^m \frac{\lambda_i²}{\sigma²}$$

$$\mathcal{L}_T(\vec{λ}) = ∑_{\mathbf{x},\mathbf{s} ∈ \mathcal{T}} \log P(\mathbf{s}| \mathbf{x}) - ∑_{i=1}^m \frac{\lambda_i²}{\sigma²}$$

\end{document}


• For \mid, thickspace rather than thinspace is inserted on either side of |. By the way, your code shows two equations, yet the screenshot displays three numbered equations. Finally, P should be set in mathrm since it's an operator. – Mico Sep 27 '16 at 12:47
• Arguments don't have to be enclosed in brackets, if they only consist of one letter, to be precise. so \mathcal L and \mathcal{L} are the same. – Ronny Sep 27 '16 at 12:50
• Yes, but my point was to insist it's not between parentheses (except some special commands). – Bernard Sep 27 '16 at 12:53
• Arguments don't have to be enclosed in bracelets, if they only consist of one token, to be precise. – Moss_the_TeXie Sep 27 '16 at 13:50