Getting error says "missing $ inserted" in latex - latex

Getting error message that there is a missing $ inserted
I've tried adding in front of n and the other one in front of \end{pmatrix} but it doesn't work.
\begin{equation} n_1=\begin{pmatrix}2\\1\\-1\end{pmatrix}\times \begin{pmatrix}3\\-1\\0\end{pmatrix} = \begin{vmatrix}$e_1&e_2&e_3$\\2&1&-1\\3&-1&0\end{vmatrix}=\begin{pmatrix}-1\\-3\\-5\end{pmatrix} \end{equation}
\begin{equation}n_2=\begin{pmatrix}1\\1\\-1\end{pmatrix}\times \begin{pmatrix}2\\-1\\0\end{pmatrix} = \begin{vmatrix}$e_1&e_2&e_3$\\1&1&-1\\2&-1&0\end{vmatrix}=\begin{pmatrix}-1\\-2\\-3\end{pmatrix}\end{equation}

The following code segment
\documentclass[a4papaer, 11pt]{article}
\usepackage[top=3.5cm, bottom=3.5cm, left=3.0cm, right=3.0cm]{geometry}
\usepackage{amsmath}
\begin{document}
\begin{equation} n_1=
\begin{pmatrix}
2\\1\\-1
\end{pmatrix}\times
\begin{pmatrix}
3\\-1\\0
\end{pmatrix} =
\begin{vmatrix}
e_1&e_2&e_3 \\
2&1&-1\\
3&-1&0
\end{vmatrix} =
\begin{pmatrix}
-1\\-3\\-5
\end{pmatrix}
\end{equation}
\begin{equation}
n_2=
\begin{pmatrix}
1\\1\\-1
\end{pmatrix}\times
\begin{pmatrix}
2\\-1\\0
\end{pmatrix} =
\begin{vmatrix}
e_1&e_2&e_3\\
1&1&-1\\
2&-1&0
\end{vmatrix}=
\begin{pmatrix}
-1\\-2\\-3
\end{pmatrix}
\end{equation}
\end{document}
results in the matrices from this image. Was that the result you were hoping for?

Related

What's the meaning of "double superscript" in overleaf?

I wrote a wrapped equation in overleaf:
\begin{equation}
\begin{aligned}
\y & = \argmin_{\y'} \sum_{i,j}\W_{ij}||\y'_i - \y'_j||_2^2 \\
& = \argmin_{\y'} \sum_{i,j}(\y'_i - \y'_j)^2\W_{ij} \\
& = \argmin_{\y'} \sum_{i,j}(\y'_i^2 + \y'_j^2 - 2\y'_i\y'_j)\W_{ij} \\
& = \argmin_{\y'} \sum_{i,j}\y'_i^2\W_{ij} + \sum_{i,j}\y'_j^2\W_{ij} - 2\sum_{i,j}\y'_i\y'_j\W_{ij} \\ & = \argmin_{\y'} \sum_{i}\y'_i^2D_{ii} + \sum_{j}\y'_j^2D_{jj} - 2\sum_{i,j}\y'_i\y'_j\W_{ij} \\ & = \argmin_{\y'} 2 (\sum_{i}\y'_i^2D_{ii} - \sum_{i,j}\y'_i\y'_j\W_{ij}) \\
& = \argmin_{\y'} 2 {\y'}^\top \L{\y'} \\
\end{aligned}
\end{equation}
The overleaf shows that there are three "double superscript" error in this code, what is that and how can I fix this (although I can compile it successfully)?
I expect to find the solution to this problem.
A double sub-/superscript error occurs when you have more than one sub-/superscript associated with an element. An example is $x^2^2$ which can be interpreted in two ways: ${x^2}^2$ or $x^{2^2}$. Mathematically they mean the same, but from a typesetting point of view they are interpreted differently:
The first - ${x^2}^2$ - sets both exponents in \scriptstyle (so they're the same size) while the second sets the exponents in staggering (reducing) sizes (first one uses \scriptstyle while the second uses \scriptscriptstyle). This difference in typesetting is rather pushed to the user to fix to avoid confusion or misinterpreting what the result should look like. A similar concept holds for subscripts (like $x_2_2$) and combinations of scripts (like `$x_2^2_2$).
What is hidden in your is the use of a prime (like $y'$) which is similar to $y^\prime$:
$x' = x^\prime \neq x\prime$
As such, $y'^2$ is similar to $y^\prime^2$ which is interpreted as a double superscript, causing the error you see.
The way around it is to appropriately group the elements to denote the way you want it represented in output. In your case, this is most likely ${y'}^2$. I've done that below, with some adjustments to other elements:
\documentclass{article}
\usepackage{mathtools}
% https://tex.stackexchange.com/q/5223/5764
\DeclareMathOperator*{\argmax}{arg\,max}
\DeclareMathOperator*{\argmin}{arg\,min}
\DeclarePairedDelimiter{\norm}{\|}{\|}
\newcommand{\y}{\mathcal{Y}}
\newcommand{\W}{\mathcal{W}}
\begin{document}
\begin{equation}
\begin{aligned}
\y & = \argmin_{\y'} \sum_{i, j} \W_{ij} \norm[\big]{ \y'_i - \y'_j }_2^2 \\
& = \argmin_{\y'} \sum_{i, j} \bigl(\y'_i - \y'_j \bigr)^2 \W_{ij} \\
& = \argmin_{\y'} \sum_{i, j} \bigl( {\y'}_i^2 + {\y'}_j^2 - 2 \y'_i \y'_j \bigr) \W_{ij} \\
& = \argmin_{\y'} \sum_{i, j}{\y'}_i^2 \W_{ij} + \sum_{i, j} {\y'}_j^2 \W_{ij} - 2\sum_{i, j} \y'_i \y'_j \W_{ij} \\
& = \argmin_{\y'} \sum_i {\y'}_i^2 D_{ii} + \sum_j {\y'}_j^2 D_{jj} - 2 \sum_{i, j} \y'_i \y'_j \W_{ij} \\
& = \argmin_{\y'} 2 \biggl( \sum_i {\y'}_i^2 D_{ii} - \sum_{i, j} \y'_i \y'_j \W_{ij} \biggr) \\
& = \argmin_{\y'} 2 {\y'}^\top \L \y'
\end{aligned}
\end{equation}
\end{document}

Line cut in \tabularx{}

I have a code below. I would like to remove the white space before the highlighted part.
\documentclass[10pt,english,8pt]{beamer}
\usetheme{default}
\usepackage{booktabs}
\usepackage{tabularx}
%\newcommand{\iid}{\stackrel{\mathrm{iid}}{\sim}}
\newcolumntype{C}{>{\centering\arraybackslash}X}
\newcolumntype{R}{>{\raggedright\arraybackslash}X}
\newcolumntype{L}{>{\raggedleft\arraybackslash}X}
\title{Beamer Template}
\author{TeXstudio Team}
\begin{document}
\begin{frame}{Frame Title}
\begin{table}[htbp]
%\begin{table}[!ht]
\centering
\scriptsize
%\caption{Title }
\label{tab:5}
\begin{tabularx}{1\textwidth}
{>{\raggedright\arraybackslash}p{2cm}
>{\centering\arraybackslash}p{1.75cm}
>{\centering\arraybackslash}p{1.75cm}
>{\centering\arraybackslash}p{1.75cm}
>{\centering\arraybackslash}p{1.75cm}
}
%\multicolumn{11}{#{}p{1\textwidth}#{}}{\footnotesize
% Caption }\\
\toprule
& Log Probability of Default & Log Recovery Rate
& Log Spread & Log Leverage \\ [0.5ex]
& (1) & (2) & (3) & (4) \\
\midrule
Log Damage
&0.017** &-0.003*** & 0.002 & -0.157 \\
&(0.0008) &(0.003) & (0.075) & (0.178) \\
Log Adaptation
&-0.157*** &0.012 &-0.463** & 0.514*** \\
&(0.021) &(0.007) &(0.182) &(0.193)\\
Log Damage X Log Adaptation
&0.006 &0.018* &-0.398 &0.535 \\
&(0.030) &(0.011) &(0.249) &(0.513)\\
Constant
&-1.227***&-0.117***&2.244***&0.101\\
&(0.026)&(0.009)&(0.187)&(0.237)\\
\midrule
Observations &926 &925 &458 &177 \\
$R^2$ &41\% &19\% &8\% &38\%\\
Adjusted $R^2$ &40\%&17\%&4\%&32\%\\
Residual Std. Error
& 0.071 & 0.025 & 0.373 & 0.264 \\
& (df = 907) & (df = 906) & (df = 439) & (df = 159)\\
F Statistic
& 35.253*** & 11.455*** & 2.104***  & 5.811***  \\
& (df = 18; 907)& (df = 18; 906)& (df = 18; 439)& (df = 17; 159)\\
\bottomrule
\addlinespace
\multicolumn{4}{#{}p{0.8\textwidth}#{}}{\footnotesize \tiny{Note: *p<0.1; **p<0.05; ***p<0.01 }} \\
\end{tabularx}
%\end{table}
\end{table}
\end{frame}
\end{document}
Log Damage X Log Adaptation is too long to fit into a single cell. The easiest fix is to manually move the second part of the phrase into the next line.
If you are using a tabularx, you should use at least one X column to make sure the table can adapt to the available space
I would use #{} in front and after the other columns to get rid of the extra space
To make reading the table easier, I would add some additional vertical space between the blocks
Are you abusing the poor X as multiplication sign? Don't!
the syntax \tiny{...} is wrong. It is a switch and does not take an argument. Use {\tiny ...} instead.
Missing math mode around the < signs
most of your table should be in math mode to get correct minus sign and proper spacing around operators, but I'm too lazy to add this in the example below...
\documentclass[10pt,english,8pt]{beamer}
\usetheme{default}
\usepackage{booktabs}
\usepackage{tabularx}
%\newcommand{\iid}{\stackrel{\mathrm{iid}}{\sim}}
\newcolumntype{C}{>{\centering\arraybackslash}X}
\newcolumntype{R}{>{\raggedright\arraybackslash}X}
\newcolumntype{L}{>{\raggedleft\arraybackslash}X}
\title{Beamer Template}
\author{TeXstudio Team}
\begin{document}
\begin{frame}{Frame Title}
\begin{table}[htbp]
%\begin{table}[!ht]
\centering
\scriptsize
%\caption{Title }
\label{tab:5}
\begin{tabularx}{1\textwidth}
{
#{}
>{\raggedright\arraybackslash}X
>{\centering\arraybackslash}p{1.75cm}
>{\centering\arraybackslash}p{1.75cm}
>{\centering\arraybackslash}p{1.75cm}
>{\centering\arraybackslash}p{1.75cm}
#{}
}
%\multicolumn{11}{#{}p{1\textwidth}#{}}{\footnotesize
% Caption }\\
\toprule
& Log Probability of Default & Log Recovery Rate
& Log Spread & Log Leverage \\ [0.5ex]
& (1) & (2) & (3) & (4) \\
\midrule
Log Damage
&0.017** &-0.003*** & 0.002 & -0.157 \\
&(0.0008) &(0.003) & (0.075) & (0.178) \\\addlinespace
Log Adaptation
&-0.157*** &0.012 &-0.463** & 0.514*** \\
&(0.021) &(0.007) &(0.182) &(0.193)\\\addlinespace
Log Damage $\times$
&0.006 &0.018* &-0.398 &0.535 \\
Log Adaptation &(0.030) &(0.011) &(0.249) &(0.513)\\\addlinespace
Constant
&-1.227***&-0.117***&2.244***&0.101\\
&(0.026)&(0.009)&(0.187)&(0.237)\\
\midrule
Observations &926 &925 &458 &177 \\
$R^2$ &41\% &19\% &8\% &38\%\\
Adjusted $R^2$ &40\%&17\%&4\%&32\%\\\addlinespace
Residual Std. Error
& 0.071 & 0.025 & 0.373 & 0.264 \\
& (df = 907) & (df = 906) & (df = 439) & (df = 159)\\\addlinespace
F Statistic
& 35.253*** & 11.455*** & 2.104*** & 5.811*** \\
& (df = 18; 907)& (df = 18; 906)& (df = 18; 439)& (df = 17; 159)\\
\bottomrule
\addlinespace
\multicolumn{4}{#{}p{0.8\textwidth}#{}}{\footnotesize {\tiny Note: *$p<0.1$; **$p<0.05$; ***$p<0.01$ }} \\
\end{tabularx}
%\end{table}
\end{table}
\end{frame}
\end{document}

How to align math equations even when some variables are missing?

I want the variables and signs aligned in a set of equations.
Desired output:
What I am trying in Mathjax:
\[
\begin{align}
2&x_1 - x_2 &+ 1.5&x_3 &= 8 \\
&x_1 &- 4&x_3 &= -1
\end{align}
\]
What I got:
So, what do I have to do to have the same alignment as in the "desired output" image?
There are a number of ways to achieve this. Below I use an array (with appropriate stretch and spacing), alignat and align coupled with eqparbox for measuring similarly-tagged boxes (this latter approach requires two compilations with every change in the largest element associated with every <tag>):
\documentclass{article}
\usepackage{amsmath,eqparbox}
%\usepackage{xparse}% If you have LaTeX2e < 2020-10-01
% https://tex.stackexchange.com/a/34412/5764
\makeatletter
% \eqmathbox[<tag>][<align>]{<math>}
\NewDocumentCommand{\eqmathbox}{o O{c} m}{%
\IfValueTF{#1}
{\def\eqmathbox###1##2{\eqmakebox[#1][#2]{$##1##2$}}}
{\def\eqmathbox###1##2{\eqmakebox{$##1##2$}}}
\mathpalette\eqmathbox#{#3}
}
\makeatother
\begin{document}
\[
\renewcommand{\arraystretch}{1.2}
\setlength{\arraycolsep}{0pt}
\begin{array}{ r c r c r c r }
2 x_1 & {}-{} & x_2 & {}+{} & 1.5 x_3 & {}={} & 8 \\
x_1 & & & {}-{} & 4 x_3 & {}={} & -7
\end{array}
\]
\begin{alignat*}{4}
2 x_1 & {}-{} & x_2 & {}+{} & 1.5 x_3 = {} && 8 \\
x_1 & & & {}-{} & 4 x_3 = {} && -7
\end{alignat*}
\begin{align*}
\eqmathbox[x1][r]{2 x_1} - \eqmathbox[x2][r]{x_2} + \eqmathbox[x3][r]{1.5 x_3} &= \eqmathbox[c][r]{8} \\
\eqmathbox[x1][r]{ x_1} \phantom{{}-{}} \eqmathbox[x2][r]{} - \eqmathbox[x3][r]{4 x_3} &= \eqmathbox[c][r]{-7}
\end{align*}
\end{document}
All yield similar output:

How To Write A Beautiful Brace In Tex

Today I see a beautiful equation:
(Sorry for cannot put the pic directly, I'm new here.)
I don't know how to write it in Tex. I try my best to write like following:
\begin{equation}
\begin{aligned}
r_{t}=\left\{
\begin{array}{crl}
1+\dfrac{\bar{R}_{Q}(t+\Delta t)-R_{Q}(t)}{2\Delta t/T_{single}}\; &+0\qquad &if\,\bar{R}_{Q}(t+\Delta t)>0,\\
0 \; &-P\qquad &if\,R_{Q}(t)\neq 0\wedge R_{Q}(t+\Delta t)=0,\\
0\; &+0\qquad &if\,R_{Q}(t)=0
\label{rforProtRwd}
\end{array}
\right.
\\
\underbrace{\hspace{10em}}_{=:r_{t}^{(1)}}\hspace{1em}\underbrace{\hspace{2em}}_{=:r_{t}^{(2)}}\hspace{17em}
\end{aligned}
\end{equation}
It is obviously that the underbrace is a little far from the main part of the equation. And actually both the method I write and the result are ugle.
So I wonder if there are some better ways to write it.
Hoping someone can help.
Here is an accurate \underbrace that matches the content within a cases environment.
\documentclass{article}
\usepackage{mathtools,eqparbox}
\begin{document}
\begin{align}
r_t = \begin{cases}
\eqmakebox[LHS]{$1 + \dfrac{\bar{R}_Q(t + \Delta t) - R_Q(t)}{2 \Delta t / T_{\text{single}}}$}
+ 0 & \text{if $\bar{R}_Q(t + \Delta t) > 0$}, \\
\eqmakebox[LHS]{$0$}
- P & \text{if $R_Q(t) \neq 0 \wedge R_Q(t + \Delta t) = 0$}, \\
\eqmakebox[LHS]{$0$}
+ 0 & \text{if $R_Q(t) = 0$}
\end{cases} \\[-1.2\normalbaselineskip]
\underbrace{\eqmakebox[LHS]{\mathstrut}}_{=:r_t^{(1)}}
\underbrace{\mathstrut\phantom{{} - P}}_{\mathclap{=:r_t^{(2)}}}
\quad\phantom{\text{if $R_Q(t) \neq 0 \wedge R_Q(t + \Delta t) = 0$},} \nonumber
\end{align}
\end{document}

How to center mathematical expression inside multi-line closed brackets?

How could I center the two lines inside the brackets. The code so far is:
\documentclass[a4paper,12pt,oneside]{report}
\usepackage{amsmath}
\begin{equation}
x_{t}^{\ast }(n)=
\left\{\!\begin{aligned}
& \hfil {x_{t}}\\
\hfil & {0,\text{ otherwise}}
\end{aligned}\right\}
\end{equation}
Here are a couple of options:
\documentclass{article}
\usepackage{amsmath,eqparbox,xparse}
% https://tex.stackexchange.com/a/34412/5764
\makeatletter
\NewDocumentCommand{\eqmathbox}{o O{c} m}{%
\IfValueTF{#1}
{\def\eqmathbox###1##2{\eqmakebox[#1][#2]{$##1##2$}}}
{\def\eqmathbox###1##2{\eqmakebox{$##1##2$}}}
\mathpalette\eqmathbox#{#3}
}
\makeatother
\begin{document}
\[
\renewcommand{\arraystretch}{1.2}
x_t^* (n) =
\left\{\begin{array}{#{} c #{}}
x_t \\
0, \text{ otherwise}
\end{array}\right\}
\]
\[
x_t^* (n) =
\left\{\begin{aligned}
\eqmathbox[cond]{x_t} \\
\eqmathbox[cond]{0, \text{ otherwise}}
\end{aligned}\right\}
\]
\end{document}
The first uses an array with a single, centred column. The second uses aligned and sets each entry inside an \eqmathbox[<tag>] with the same <tag>. eqparbox then finds the maximum width of those elements with the same <tag>, and sets each element inside a box of that width (default <align>ment is centred \eqmathbox[<tag>][<align>]{<stuff>}).

Resources