Adding a caption to an equation in LaTeX - latex

Well, it seems simple enough, but I can't find a way to add a caption to an equation.
The caption is needed to explain the variables used in the equation, so some kind of table-like structure to keep it all aligned and pretty would be great.

The \caption command is restricted to floats: you will need to place the equation in a figure or table environment (or a new kind of floating environment). For example:
\begin{figure}
\[ E = m c^2 \]
\caption{A famous equation}
\end{figure}
The point of floats is that you let LaTeX determine their placement. If you want to equation to appear in a fixed position, don't use a float. The \captionof command of the caption package can be used to place a caption outside of a floating environment. It is used like this:
\[ E = m c^2 \]
\captionof{figure}{A famous equation}
This will also produce an entry for the \listoffigures, if your document has one.
To align parts of an equation, take a look at the eqnarray environment, or some of the environments of the amsmath package: align, gather, multiline,...

You may want to look at http://tug.ctan.org/tex-archive/macros/latex/contrib/float/ which allows you to define new floats using \newfloat
I say this because captions are usually applied to floats.
Straight ahead equations (those written with $ ... $, $$ ... $$, begin{equation}...) are in-line objects that do not support \caption.
This can be done using the following snippet just before \begin{document}
\usepackage{float}
\usepackage{aliascnt}
\newaliascnt{eqfloat}{equation}
\newfloat{eqfloat}{h}{eqflts}
\floatname{eqfloat}{Equation}
\newcommand*{\ORGeqfloat}{}
\let\ORGeqfloat\eqfloat
\def\eqfloat{%
\let\ORIGINALcaption\caption
\def\caption{%
\addtocounter{equation}{-1}%
\ORIGINALcaption
}%
\ORGeqfloat
}
and when adding an equation use something like
\begin{eqfloat}
\begin{equation}
f( x ) = ax + b
\label{eq:linear}
\end{equation}
\caption{Caption goes here}
\end{eqfloat}

As in this forum post by Gonzalo Medina, a third way may be:
\documentclass{article}
\usepackage{caption}
\DeclareCaptionType{equ}[][]
%\captionsetup[equ]{labelformat=empty}
\begin{document}
Some text
\begin{equ}[!ht]
\begin{equation}
a=b+c
\end{equation}
\caption{Caption of the equation}
\end{equ}
Some other text
\end{document}
More details of the commands used from package caption: here.
A screenshot of the output of the above code:

Related

Mixing text and equations in latex

I am working on some algorithm documenation for a project and trying to write out the equations in latex.
The one problem I am encountering and have not found a nice way (assuming there is one) is mixing text and equations in a single line.
Here is an example of what I am doing (and later how I am doing it).
I am defining the equation, and than what each variable means (left aligned text hence the &).
The latex code to generate this
\begin{equation}
A = 3B * 4C + 5D
\end{equation}
Where:
\begin{flalign*}
&A = Something \: cool\\
&B = Something \: cooler\\
&C = Something \: even \: cooler!!\\
\end{flalign*}
My questions are:
Is there a better way to do spaces in between words besides putting \: everywhere?
If I dont put the \: I get this below, all the words are combined?
Is this the most latex idiomatic way to acheive this? Am I missing something that could help me?
So I can get the output the way I want, I just want to make sure its "correct" before I get to deep.
You should never set whole words in math mode. Besides the obvious problem with spaces you noticed, this will also completely mess up the kerning between the letters.
Instead you can use the \text{...} macro from the amsmath package.
The amsmath package also provides the \intertext macro, which you could use to insert Where: while retaining alignment of the equal signs in the equations above and below:
\documentclass{article}
\usepackage{amsmath}
\begin{document}
\begin{flalign}
A &= 3B \cdot 4C + 5D\\
\intertext{Where:}
A &= \text{Something cool}\notag\\
B &= \text{Something cooler}\notag\\
C &= \text{Something even cooler!!}\notag
\end{flalign}
\end{document}

LaTex how to edit this equation?

I'm having issues turning this word equation into a LaTex equation. It's coming out looking dodgy, please help!
I added a screen shot of the equation I want, and what I end up getting when I copy and paste into LaTex:
WORD:
LATEX CODE:
\mathrm{=\ }\mathrm{C}_\mathrm{0}\mathrm{[1-}6(Dt)1/2aπ2-3Dta2] + 12(Dt)1/2an = 1∞exp(na(Dt)1/2)
and therefore nothing comes out and LaTex doesn't let me run it.
This is absolutely not a proper LaTeX equation code. I don't know what you know about LaTeX, but you cannot just copy and paste from Word or any software to you LaTex editor. Plus, you need to provide your full code for anyone being able to help you.
Anyway, running this MWE should work :
\documentclass[11pt, a4paper, twoside]{report}
% ===== PACKAGES DECLARATION =====
\usepackage{mathtools} % Replaces amsmaths + more features
\usepackage{amsfonts} % Maths fonts package
% ===== DOCUMENT BODY =====
\begin{document}
\begin{equation} % optional : use the "equation*" environment to remove equation number
% optional : use traditional math font by removing the \mathrm{} command
\mathrm{X = C_0 \left[ 1 - \frac{6(Dt)^{1/2}}{a \pi^2} - \frac{3Dt}{a^2}\right] + \frac{12(Dt)^{1/2}}{a} \sum_{n=1}^\infty \exp\left(\frac{na}{(Dt)^{1/2}} \right)}
% optional : remove auto-sized brackets by removing the \left and \right commands
\end{equation}
\end{document}
As written in the code, you may want to remove the equation number and the big auto-sized brackets (that are more readable in my opinion). Just remove the corresponding commands. Also, you should consider using the "normal" math font and not the roman one that is clearly different from the text and helps the reader to separate equations from inline small expressions you could insert in your document.
One first sketch:
\documentclass{article}
\begin{document}
\[
C_0\left[1-\frac{6(Dt)^{\frac{1}{2}}}{a\pi^2}-\frac{3Dt}{a^2}\right]+%
\frac{12(Dt)^{\frac{1}{2}}}{a}\sum^{\infty}_{n=1}%
\exp\left(\frac{na}{(Dt)^{\frac{1}{2}}}\right)
\]
\end{document}
No packages required. The output:
Than you can tune the math fonts and anything else.

latex error Missing $ inserted

I'm trying to use LaTeX to write my math notes. But I have some problems.
The error is written:
LaTeX error Missing $ inserted
And it doesn't compile correctly:
\documentclass[french,12pt,a4paper]{article}
\usepackage[french]{babel}
\usepackage[T1]{fontenc}
\usepackage[utf8]{inputenc}
\usepackage{amsmath}
\usepackage{fullpage}
\author{
Alexandre Monterroso\\
Université de Fribourg\\
}
\newtheorem{de}{Définition}[subsection]
\newtheorem{theo}{Théorème}[section]
\newtheorem{prop}[theo]{Proposition}
\begin{document}
\section{Combinatoris}
\subsection{Ordered choices}
\begin{itemize}
\item With repetition
\end{itemize}
\begin{theo}
The number of lists (a_{1}, ..., a_{k}) of k not necessarly distincts objects elements (ie. with repetition) from a set n elements is a^{k} (n, k \epsilon N).
\end{theo}
\end{document}
You need to specify math-related content in math mode. So, use
The number of lists ($a_1, \dots, a_k$) of~$k$ not necessarily dist ...
$...$ initiated and closes in-line math mode, while \[...\] initiates and closes display math mode.

Typesetting a large matrix in LaTeX

I have a 3x12 matrix I'd like to input into my LaTeX (with amsmath) document but LaTeX seems to choke when the matrix gets larger than 3x10:
\begin{equation}
\textbf{e} =
\begin{bmatrix}
1&1&1&1&0&0&0&0&-1&-1&-1&-1\\
1&-1&0&0&1&1&-1&-1&0&0&1&-1\\
0&0&1&-1&1&-1&1&-1&1&-1&0&0
\end{bmatrix}
\end{equation}
The error: Extra alignment tab has been changed to \cr. tells me that I have more & than the bmatrix environment can handle. Is there a proper way to handle this? It also seems that the alignment for 1's and the -1's are different, is that also expected of the bmatrix?
From the amsmath documentation (texdoc amsmath):
The amsmath package provides some
environments for matrices beyond the
basic array environment of LATEX. The
pmatrix, bmatrix, Bmatrix, vmatrix and
Vmatrix have (respectively) ( ), [
], { }, | |, and ∥
∥ delimiters built in. For naming
consistency there is a matrix
environment sans delimiters. This is
not entirely redundant with the array
environment; the matrix environments
all use more economical horizontal
spacing than the rather prodigal
spacing of the array environment.
Also, unlike the array environment,
you don’t have to give column
specifications for any of the matrix
environments; by default you can have
up to 10 centered columns. (If you
need left or right alignment in a
column or other special formats you
must resort to array.)
i.e. bmatrix defaults to a 10 column maximum.
A footnote adds
More precisely: The maximum number of
columns in a matrix is determined by
the counter MaxMatrixCols (normal
value = 10), which you can change if
necessary using LATEX’s \setcounter or
\addtocounter commands.
If you came to this page looking for the exact command (thanks to Scott Wales for the answer), you want this in your preamble:
\setcounter{MaxMatrixCols}{20}
Where you can replace 20 with the maximum number of columns you want.
The answer by Scott is correct, but I've since learned you can override the alignment. Taken from http://texblog.net/latex-archive/maths/matrix-align-left-right/
\makeatletter
\renewcommand*\env#matrix[1][c]{\hskip -\arraycolsep
\let\#ifnextchar\new#ifnextchar
\array{*\c#MaxMatrixCols #1}}
\makeatother
Now allows the command:
\begin{bmatrix}[r] ....
to have right-alignment!
Instead of a bmatrix you can use +bmatrix from the tabularray package:
\documentclass{article}
\usepackage{tabularray}
\UseTblrLibrary{amsmath}
\begin{document}
\begin{equation}
\textbf{e} =
\begin{+bmatrix}
1&1&1&1&0&0&0&0&-1&-1&-1&-1\\
1&-1&0&0&1&1&-1&-1&0&0&1&-1\\
0&0&1&-1&1&-1&1&-1&1&-1&0&0
\end{+bmatrix}
\end{equation}
\end{document}

Adding an equation or formula to a figure caption in LaTeX

I have a figure in LaTeX with a caption to which I need to add a formula (equation* or displaymath environments). For example:
\documentclass[12pt]{article}
\begin{document}
\begin{figure}[tbph]
\begin{center}
%...
\end{center}
\caption{As you can see
\begin{displaymath}4 \ne 5\end{displaymath}
}
\label{fig:somefig}
\end{figure}
\end{document}
This makes pdflatex angry, though it will produce a PDF.
! Argument of \#caption has an extra }.
<inserted text>
\par
l.9 }
What's the right way to go about adding an equation to a figure caption?
NOTE: Please do not suggest simply using the $ ... $ math environment;
the equation shown is a toy example; my real equation is much more intricate.
See also:
Adding a caption to an equation in LaTeX (the reverse of this question)
Using the package "caption":
\begin{figure}
\begin{center}
...
\captionsetup{singlelinecheck=off}
\caption[.]{
\begin{displaymath}
assoc\_meaning(\lambda x_{SBJ}. followed(x,y) \&actor(x) \nonumber \&actor(y),\lambda x_{SBJ}. maintained(x,\nonumber <(dist\_from(y),1))
\end{displaymath}}
\end{center}
\end{figure}
The square brackets following \caption aren't optional, but leaving them off won't cause an error that looks any different than the one before you added \usepackage{caption} and \captionsetup{...}.
I'm not sure why you do not want to use the $ ... $ solution, because of fractions?
If so, you can use \dfrac instead of \frac.
I would try $ \displaystyle \dfrac{1}{2} \cdot \sum_{i=0}^n i$, i.e. use the \displaystyle command.

Resources