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.
Related
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?
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:
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?
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}
I use the following code to create a box for equations. It uses Tikz to create the box.
\documentclass[a4paper]{article}
\usepackage{tikz}
\usepackage{amsmath}
\usepackage{amsmath,amssymb}% pour les maths
\usepackage{enumitem}
\usepackage{varwidth}
\usepackage{listings}
\usetikzlibrary{calc}
\newcommand{\mybox}[4][\textwidth-\pgfkeysvalueof{/pgf/inner xsep}-2mm]{%
\begin{figure}[!h]
\centering
\begin{tikzpicture}
\node[line width=.5mm, rounded corners, draw=#2, inner ysep=10pt, text width=#1, outer sep=0] (one) {\vspace*{15pt}\\\begin{varwidth}{\textwidth}#4\end{varwidth}};
\node[text=white,anchor=north east,align=center, minimum height=20pt] (two) at (one.north east) {#3 \hspace*{.5mm}};
\path[fill=#2]
(one.north west|-two.west) --
($(two.west)+(-1.5cm,0)$)
to[out=0,in=180] (two.south west) --
(two.south east) [rounded corners] --
(one.north east) --
(one.north west) [sharp corners] -- cycle;
\node[text=white,anchor=north east,align=center, minimum height=20pt, text height=2ex] (three) at (one.north east) {#3 \hspace*{.5mm}};
\end{tikzpicture}
\end{figure}
}
\begin{document}
\mybox{green!70!black}{The Caption}{
\begin{enumerate}
\item Show that\\
$\displaystyle D_2f(x,y) = \frac{\partial {}}{\partial{y}} \biggl( \int_0^xg_1 (t,0) \ dt + \int_0^y g_2(x,s) \ ds \biggr)$
\item prove that\\
$\displaystyle \biggl(\forall x\in\mathbb{R} \biggr)\biggl(\forall y \in \mathbb{R} \biggr) x\neq y\, \text{ and } \, x+y \neq 2 \implies x^{2}-2x \neq y^2-2y$
\end{enumerate}
}
\end{document}
but Now I want to put listing inside the box
When I put the listing inside the mbox I got an error
\begin{lstlisting}[language=R]
fun1 <- function(data, data.frame, graph=TRUE, limit=20, ...) {
[omitted statements]
if (graph)
par(pch="*", ...)
[more omissions]
}
\end{lstlisting}
the error is
! Argument of \lst#next has an extra }.
You cannot use listings inside arguments directly. You either need to escape and properly prepare the verbatim code or store it somewhere else. See section 6.1 of the listing manual for more information.
Store the listing in a box
\begin{document}
\begin{lrbox}{\mylisting}
\begin{lstlisting}[language=R]
fun1 <- function(data, data.frame, graph=TRUE, limit=20, ...) {
[omitted statements]
if (graph)
par(pch="*", ...)
[more omissions]
}
\end{lstlisting}
\end{lrbox}
\mybox{green!70!black}{The Caption}{
\usebox\mylisting
}
\end{document}
Store the listing in an external file
\lstinputlisting{external_file.R}
Add line feeds ^^J
\begin{lstlisting}[language=R]^^J
fun1 <- function(data, data.frame, graph=TRUE, limit=20, ...) {^^J
[omitted statements]^^J
if (graph)^^J
par(pch="*", ...)^^J
[more omissions]^^J
}^^J
\end{lstlisting}
Note: You will probably need to resize your box for that long first line of code.