Latex equation reference not working as expected - latex

I am preparing a report and found out \label and \eqref are not working for equations properly. \eqref giving a random equation number and not the exact equation number in the report. I have used \numberwithin{equation}{section} numbering for equations, which means if the equation is in section 5, it will have number from 5.1, 5.2 and so on. But in \eqref only 5 is coming and not the exact number of the equation. Is there any solution for this?
Equation number \eqref{eq:1}.
\begin{fleqn}[150pt] \label{eq:1}
\begin{equation}
\begin{aligned}
\Delta l^e = u^e_2 - u^e_1
\end{aligned}
\end{equation}
\end{fleqn}

Your label needs to be inside the equation:
\documentclass{article}
\usepackage{mathtools}
\numberwithin{equation}{section}
\begin{document}
\section{title}
Equation number \eqref{eq:1}.
\begin{equation}
\begin{aligned}
\Delta l^e = u^e_2 - u^e_1
\end{aligned}
\label{eq:1}
\end{equation}
\end{document}
If you really must use the nccmath package, you could roll back to an older latex version:
\RequirePackage[2019/01/01]{latexrelease}
\documentclass{article}
\usepackage{nccmath}
\numberwithin{equation}{section}
\begin{document}
\section{title}
Equation number \eqref{eq:1}.
\begin{fleqn}[150pt]
\begin{equation}
\begin{aligned}
\Delta l^e = u^e_2 - u^e_1
\end{aligned}
\label{eq:1}
\end{equation}
\end{fleqn}
\end{document}

Related

Latex equation with unequal space

I have typed code for set of equation but I am not sure the problem as there is different spacing for it from the left, can you help me correct this uneven spacing
\begin{eqnarray}
L(x) &=& T_e(x) - T_i(x)\\
\label{eq:latencymsg}
L_{avg} &=& 1/N \sum_{n=1}^{N} L(x)\\
\label{eq:latencymsgavg}
\begin{cases}
\frac{1}{b-a}&\text{for $L(x)\in[a,b]$}\\
0&\text{otherwise}\\
\end{cases}\\
\begin{cases}
0&\text{for $L(x)<a$}\\
\frac{x-a}{b-a}&\text{for $L(x)\in[a,b)$}\\
1&\text{for $L(x)\ge b$}
\end{cases}
\end{eqnarray}
Got it: do not nest cases into eqnarray, just use two more equation environments.
\documentclass{article}
\usepackage{amsmath}
\begin{document}
\begin{eqnarray}
L(x) &=& T_e(x) - T_i(x)\\
\label{eq:latencymsg}
L_{avg} &=& 1/N \sum_{n=1}^{N} L(x)
\label{eq:latencymsgavg}
\end{eqnarray}
\begin{equation}
\begin{cases}
\frac{1}{b-a}&\text{for $L(x)\in[a,b]$}\\
0&\text{otherwise}\\
\end{cases}
\end{equation}
\begin{equation}
\begin{cases}
0&\text{for $L(x)<a$}\\
\frac{x-a}{b-a}&\text{for $L(x)\in[a,b)$}\\
1&\text{for $L(x)\ge b$}
\end{cases}
\end{equation}
\end{document}
Labelng is not the same because I do not have chapters 1, 2 and 3 in my MWE, but:
Eventually, add two more labels to cite equations number three and four.

LaTeX align environment missing $ inserted

I'm using an Align environment inside of a proof, and I'm getting the error "Missing $ inserted." I've commenting out all but the first line of the align makes the error go away, but I still can't figure out what the problem is. No solution I could find works. Am I missing something?
\documentclass[12pt,letterpaper]{article}
\usepackage{ifpdf, enumerate}
\usepackage{mla}
\usepackage{gb4e}
\usepackage{amsfonts}
\usepackage{amsmath}
\usepackage{amsthm}
\usepackage{stmaryrd}
\newcommand{\tuple}[1]{\ensuremath{\left \langle #1 \right \rangle }}
\newcommand{\evaluation}[2][]{\ensuremath{\llbracket #2\rrbracket^{#1}}}
\begin{document}
\begin{proof}
\begin{align*}
\evaluation[\mathbb{M}]{(Mx)(D (x) \& V (x))} = 1 \iff \\
\evaluation[\mathbb{M}]{\lbrack \alpha / x \rbrack (D (x) \& V (x))} = 1 \text{for most constants \alpha} \\
\evaluation[\mathbb{M}]{(D (\alpha) \& V (\alpha))} = 1 \text{for most constants \alpha } \\
\evaluation[\mathbb{M}]{D (\alpha)} = 1 \text{and} \evaluation[\mathbb{M}]{V (\alpha)} = 1 \text{for most constants \alpha } \\
F(\alpha) \in F(D) \text{and} F(\alpha) \in F(V) \text{for most constants \alpha } \\
\intertext{Examining the definitions of the model, we see that there are only four entities that are both in $F(D)$ and $F(V)$: Alice, Bob, Colin, and David. Because this is not more than half of the total twelve entities in the universe, our naive definition of ``most'' is not satisfied. Therefore the statement is false, which implies that the truth value of the origenal proposition is not 1.}
\end{align*}
\end{proof}
\end{document}
Here is what the error message looks like.
./test.tex:24: Missing $ inserted.
<inserted text>
$
l.24 \end{align*}
?
You've missed only one tricky fact: \alpha only works in math mode, so if you replace all occurrences of
\text{for most constants \alpha} \\
(lines 18, 19, 20, 21) with
\text{for most constants }\alpha \\
then your tex will compile sweetly. Also:
\text{for most constants}\ \alpha \\
or:
\text{for most constants $\alpha$} \\
as you already did in line 22 of your code (...$F(D)$ and $F(V)$...).
This answer is not applicable to this exact case, but the given error can also pop up if the "amsmath" package is not being used. It becomes tricky because the whole code looks alright except I had forgotten to add the \usepackage{amsmath} line in the beginning of the document.

How to number gathered equations using single number

I want to number gathered equations, but single number for several equations. Following is the code I've used. But it results numbering both equations. I want to equations to be justified, not right aligned. Spilt and align environment right align the equations. Is there any way to do this?
\begin{gather}
\eta_{c1} P_{pv}(k) + \eta_{c2} P_{bat}(k) \leq P_{conG,rate} \\\
P_{grid} + P_{load} \geq -P_{conG,rate}
\end{gather}
After \ you can put \nonumber this way you can avoid having both equations numbered
As said by Good Luck, you can add \nonumber at the end of the line before \\.
A problem I encountered was that when adding a \label{label_name} the Eq.~\ref{label_name} showed as (for example) Eq.2.5 instead of Eq.2. I've managed to sort this by moving the \label{label_name} to the row below \begin{gather}. So for example my code would look like this:
\begin{gather}
\label{labe_name}
e=\sum_{n=0}^{\infty}{\frac{1}{n!}}\\
\begin{aligned}
&\textit{\small Where:}\nonumber\\
&\textit{\small $1^{st}$ line and more explanations to fill the line} \nonumber\\
&\textit{\small $2^{nd}$ line}\nonumber\\
&\textit{\small ext \dots}\nonumber\\
\end{aligned}
\end{gather}
Make sure to have this packages:
\usepackage{amsmath,amssymb}
Here is a cleaner solution than using \nonumber. Using gathered or aligned environments inside an equation environment groups the equations and assigns one number to them. This is recommended by the amsmath user guide.
\documentclass{article}
\usepackage{amsmath}
\begin{document}
\begin{equation}
\begin{gathered}
\eta_{c1} P_{pv}(k) + \eta_{c2} P_{bat}(k) \leq P_{conG,rate}
\\
P_{grid} + P_{load} \geq -P_{conG,rate}
\end{gathered}
\end{equation}
\begin{equation}
\begin{aligned}
\eta_{c1} P_{pv}(k) + \eta_{c2} P_{bat}(k) &\leq P_{conG,rate}
\\
P_{grid} + P_{load} &\geq -P_{conG,rate}
\end{aligned}
\end{equation}
\end{document}

How can I have linebreaks in my long LaTeX equations?

My equation is very long. How do I get it to continue on the next line rather than go off the page?
If your equation does not fit on a single line, then the multline (note that that's multline without an "i", not "multiline") environment probably is what you need:
\begin{multline}
first part of the equation \\
= second part of the equation
\end{multline}
If you also need some alignment respect to the first part, you can use split:
\begin{equation}
\begin{split}
first part &= second part #1 \\
&= second part #2
\end{split}
\end{equation}
Both environments require the amsmath package.
See also aligned as pointed out in an answer below.
Not yet mentioned here, another choice is environment aligned, again from package amsmath:
\documentclass{article}
\usepackage{amsmath}
\begin{document}
\begin{equation}
\begin{aligned}
A & = B + C\\
& = D + E + F\\
& = G
\end{aligned}
\end{equation}
\end{document}
This outputs:
Without configuring your math environment to clip, you could force a new line with two backslashes in a sequence like this:
Bla Bla \\ Bla Bla in another line
The problem with this is that you will need to determine where a line is likely to end and force to always have a line break there. With equations, rather than text, I prefer this manual way.
You could also use \\* to prevent a new page from being started.
If it is inline equation, then use \allowbreak. Use it like:
$x_1,x_2,x_3,\allowbreak x_4,x_5$.
Latex will break equation in this place only if necessary.
There are a couple ways you can deal with this. First, and perhaps best, is to rework your equation so that it is not so long; it is likely unreadable if it is that long.
If it must be so, check out the AMS Short Math Guide for some ways to handle it. (on the second page)
Personally, I'd use an align environment, so that the breaking and alignment can be precisely controlled. e.g.
\begin{align*}
x&+y+\dots+\dots+x_100000000\\
&+x_100000001+\dots+\dots
\end{align*}
which would line up the first plus signs of each line... but obviously, you can set the alignments wherever you like.
I think I usually used eqnarray or something. It lets you say
\begin{eqnarray*}
x &=& blah blah blah \\
& & more blah blah blah \\
& & even more blah blah
\end{eqnarray*}
and it will be aligned by the & &... As pkaeding mentioned, it's hard to read, but when you've got an equation thats that long, it's gonna be hard to read no matter what... (The * makes it not have an equation number, IIRC)
I used the \begin{matrix}
\begin{equation}
\begin{matrix}
line_1 \\
line_2 \\
line_3
\end{matrix}
\end{equation}
multline is best to use. Instead, you can use dmath, split as well.
Here is an example:
\begin{multline}
{\text {\bf \emph {T(u)}}} ={ \alpha *}{\frac{\sum_{i=1}^{\text{\bf \emph {I(u)}}}{{\text{\bf \emph {S(u,i)}}}* {\text {\bf \emph {Cr(P(u,i))}}} * {\text {\bf \emph {TF(u,i)}}}}}{\text {\bf \emph {I(u)}}}} \\
+{ \beta *}{\frac{\sum_{i=1}^{\text{\bf \emph {$I_h$(u)}}}{{\text{\bf \emph {S(u,i)}}}* {\text {\bf \emph {Cr(P(u,i))}}} * {\text {\bf \emph {TF(u,i)}}}}}{\text {\bf \emph {$I_h$(u)}}}}
\end{multline}
This worked for me while using mathtools package.
\documentclass{article}
\usepackage{mathtools}
\begin{document}
\begin{equation}
\begin{multlined}
first term \\
second term
\end{multlined}
\end{equation}
\end{document}
Use eqnarray and \nonumber
example:
\begin{eqnarray}
sample = R(s,\pi(s),s') + \gamma V^{\pi} (s') \nonumber \\
\label{eq:temporal-difference}
V^{\pi}_{k+1}(s) = (1-\alpha)V^{\pi}(s) - \alpha[sample]
\end{eqnarray}
SIMPLE ANSWER HERE
\begin{equation}
\begin{split}
equation \\
here
\end{split}
\end{equation}
To solve this issue, I used the array environment inside the equation environment like this:
\begin{equation}
\begin{array}{r c l}
first Term&=&Second Term\\
&=&Third Term
\end{array}
\end{equation}
You do not need any extra package to do this:
\begin{equation}
\begin{gathered}
first formula\\
second formula
\end{gathered}
\end{equation}

How do I put a question mark above \leq?

How can I put a question mark above a less-than-or-equal-to symbol(\leq) in LaTeX?
You can use stackrel:
\begin{equation}
2 \stackrel{?}{\le} 3
\end{equation}
\end{document}
Or, if you use the amsmath package, you can use overset as follows:
\documentclass{article}
\usepackage{amsmath}
\begin{document}
\begin{equation}
2 \overset{?}{\le} 3
\end{equation}
\end{document}
\stackrel{\text{\tiny ?}}{=}
Use the accents package. You can do more fun stuff with TeX primitives, but here's the easy and most flexible way:
\documentclass{article}
\usepackage{accents}
\newcommand{\qleq}{\accentset{?}{\leq}}
\begin{document}
Test: $a \qleq b$.
\end{document}

Resources