latex pseudo-code can not display correctly - latex

I want to add pseudo-code in an rst file, but on the HTML page it says 'unknown environment {algorithm}'.
In conf.py, I added:
latex_engine = 'xelatex'
latex_elements = {
'preamble':
r'''
\usepackage{algorithm}
\usepackage{algorithmic}
\usepackage{amsmath}
\usepackage{cancel}
\usepackage[verbose=true,letterpaper]{geometry}
''',
}
latex_show_urls = 'footnote'
And in rst file, I added:
.. math::
:nowrap:
% This quicksort algorithm is extracted from Chapter 7, Introduction to Algorithms (3rd edition)
\begin{algorithm}
\caption{Quicksort}
\begin{algorithmic}
\PROCEDURE{Quicksort}{$A, p, r$}
\IF{$p < r$}
\STATE $q = $ \CALL{Partition}{$A, p, r$}
\STATE \CALL{Quicksort}{$A, p, q - 1$}
\STATE \CALL{Quicksort}{$A, q + 1, r$}
\ENDIF
\ENDPROCEDURE
\PROCEDURE{Partition}{$A, p, r$}
\STATE $x = A[r]$
\STATE $i = p - 1$
\FOR{$j = p$ \TO $r - 1$}
\IF{$A[j] < x$}
\STATE $i = i + 1$
\STATE exchange
$A[i]$ with $A[j]$
\ENDIF
\STATE exchange $A[i]$ with $A[r]$
\ENDFOR
\ENDPROCEDURE
\end{algorithmic}
\end{algorithm}
Did I use the wrong context? Or something else?

Related

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:

Scale an equation* to fit exact page width

I have a beamer presentation with lots of equations that are too big for a line.
I want to automatically scale them to fit.
The solution to Scale an equation to fit exact page width by https://stackoverflow.com/users/3383640/suuuehgi was great.
However I do not want equation numbers - I want this same resizing to apply to all equation* bits.
I can't figure out how to adjust the code to deal with this. Help much appreciated!
The following code uses a slightly improved approach, noting that content within a display equation is set using \displaystyle (not contained within the measurement in the linked answer). In principle, the approach is the same:
The content is measured horizontally.
The content is resized to fit if it is wider than \linewidth.
The equation* shorthand is redefined to also accommodate this new approach.
\documentclass{beamer}
\usepackage{environ}
\newlength{\myl}
\expandafter\let\expandafter\origequation\csname equation*\endcsname
\expandafter\let\expandafter\endorigequation\csname endequation*\endcsname
\long\def\[#1\]{\begin{equation*}#1\end{equation*}}
\RenewEnviron{equation*}{
\settowidth{\myl}{$\displaystyle\BODY$} % calculate width and save as \myl
\origequation
\ifdim\myl>\linewidth
\resizebox{\linewidth}{!}{$\displaystyle\BODY$}% \myl > \linewidth
\else
\BODY % \myl <= \linewidth
\fi
\endorigequation
}
\begin{document}
\begin{frame}
\begin{equation*}
f(x) = ax^2 + bx + c
\end{equation*}
\begin{equation*}
g(x) = ax^2 + bx + c
- ax^2 - bx - c
+ ax^2 + bx + c
- ax^2 - bx - c
+ ax^2 + bx + c
\end{equation*}
\[
h(x) = \int_a^b \frac{c}{d} x\,\mathrm{d}x
\]
\[
i(x) = \int_a^b \frac{c}{d} x\,\mathrm{d}x
- \int_a^b \frac{c}{d} x\,\mathrm{d}x
+ \int_a^b \frac{c}{d} x\,\mathrm{d}x
- \int_a^b \frac{c}{d} x\,\mathrm{d}x
+ \int_a^b \frac{c}{d} x\,\mathrm{d}x
- \int_a^b \frac{c}{d} x\,\mathrm{d}x
+ \int_a^b \frac{c}{d} x\,\mathrm{d}x
\]
\end{frame}
\end{document}

Latex writ only until the middle of the page

am try to type a a pseudo code of an algorithme in latex it work fine but it just writ until the midle . and the rest stay empty .. here is the source
\documentclass{IEEETran}
\usepackage{algpseudocode}
\usepackage{algorithm}
\begin{document}
\begin{algorithm}
\caption{GPX operator}
\hspace*{\algorithmicindent} \textbf{Input} s1 = $\lbrace V^{1}_{1}; ... ; V^{1}_{k}\rbrace$ and s2 = $\lbrace V^{2}_{1}; ... ; V^{2}_{k}\rbrace$ - parent solutions. \\
\hspace*{\algorithmicindent} \textbf{Output} $ s = \lbrace V_{1}; ... ; V_{k}\rbrace$ - offspring solution. \\
\begin{algorithmic}[1]
\For {$l(1\leq l \leq k)$}
\If{($l$ is odd)}
\State A=1
\Else
\State A=2;
\EndIf
\State choose $i$ such that $V^{A}_{i}$ has a maximum cardinality
\State $V_{l}$=$V^{A}_{i}$
\State remove the vertices of $V_{l}$ from $s1$ and $s2$
\EndFor
\State Assign randomly the vertices of $V - (V_{1} \cup ... \cup V_{k})$
\end{algorithmic}
\end{algorithm}
\end{document}
and here is what i get :
Default mode of IEEEtran is twocolumn. Compile with \documentclass[onecolumn]{IEEEtran} if it's what you want.

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 display two-rows bracket in Latex?

Does anyone know how to modify the following string in order to display the two-lines bracket?
str = '$$c_i =\{\begin{array}{l l} 1 \quad L\left(Q_i\right) < 0 \\ 0 \quad L\left(Q_i\right) \geq 0 \\ \end{array}$$';
The current output is the following:
The sign '{' has to embrace both rows (1 and 0).
$$c_i =\begin{cases} 1 & L\left(Q_i\right) < 0 \\
0 & L\left(Q_i\right) \geq 0
\end{cases}$$
The tex file should have \usepackage{amsmath} in the preamble.
This is derived from Niall Murphy's answer, "tidied up" a bit:
\[
c_i =
\begin{cases}
1 & L (Q_i) < 0 \\
0 & L (Q_i) \geq 0
\end{cases}
\]
Note that the "\" becomes "\\", and I've removed the \left and \right parenthesis modifiers, which introduce unwanted (I think) space between L and (.
Try add \left before \{ and \rigth. at the end.
It should look like
$$\alpha_t = \left \{ {{\sqrt{\frac1N},\; t = 0 } \atop {\sqrt{\frac2N},\; t \ne 0 } } \right.$$
or
$$
\left\{\begin{tabular}{l} \textbf{Y} = 0,299\textbf{R} + 0,587\textbf{G} + 0,114\textbf{B} \\ \textbf{Cb} = 128 + 0,5\textbf{R} - 0,418688\textbf{G} - 0,081312\textbf{B} \\ \textbf{Cr} = 128 - 0,168736\textbf{R} - 0,331264\textbf{G} - 0,53\textbf{B} \end{tabular} \right.
$$

Resources