I've written the code, but line number are not proper, the code looks like this:
\begin{algorithm}[H]
\caption{Algorithm}
\begin{algorithmic}[1]
\renewcommand{\algorithmicrequire}{\textbf{Input:}}
\renewcommand{\algorithmicensure}{\textbf{Output:}}
\REQUIRE $Graph\ G(V, E)$
\STATE $\textbf{\textit{function}}\ $
\Do
\State Something
\doWhile
\end{algorithmic}
\label{algo1}
\end{algorithm}
The output of above code looks like this:
How I can get the proper numbering? Or is there any simpler way to write do while loop in latex?
Try the algorithm2e package. ;)
\documentclass[a4paper, 11pt]{article}
\usepackage[norelsize, linesnumbered, ruled, lined, boxed, commentsnumbered]{algorithm2e}
\begin{document}
\begin{algorithm}[H]
\SetAlgoLined
\LinesNumbered
\SetKwInOut{Input}{Input}
\Input{$Graph\ G(V, E)$}
\SetKwProg{Function}{function}{}{end}
\SetKwRepeat{Do}{do}{while}
\Function{function(param: int) : int}{
\Do{done = false}{ something }
}
\caption{Algorithm}
\end{algorithm}
\end{document}
Related
I code this in a .tex file.
\begin{lstlisting}
% This is the syntax for inserting code.
\begin{lstlisting}
\end{lstlisting}
\end{lstlisting}
And this is what I expect:
% This is the syntax for inserting code.
\begin{lstlisting}
\end{lstlisting}
Unfortunately, this is not feasible. What is the correct format?
You can use another name for the lstlisting:
\documentclass{article}
\usepackage{listings}
\lstnewenvironment{mylisting}{}{}
\begin{document}
\begin{mylisting}
% This is the syntax for inserting code.
\begin{lstlisting}
\end{lstlisting}
\end{mylisting}
\end{document}
I'm trying to save space, reduce both margins of the algorithm in 1, and add a comment after the "do" in line 1 (see the problem in red). All my tentatives failed. I only could place the comment between the "SemCompositeIndex" and the "do."
Its compilable code follows:
\documentclass{article}
\usepackage{mdframed}
\usepackage[noend,linesnumbered,ruled,vlined]{algorithm2e}
\makeatletter
%Remove right hand margin in algorithm
\patchcmd{\#algocf#start}% <cmd>
{-1.5em}% <search>
{0pt}% <replace>
{}{}% <success><failure>
\makeatother
\begin{document}
\begin{algorithm}
\SetAlgoLined
\SetKwInOut{Input}{input}
\SetKwInOut{Output}{output}
\Input{SemCompositeIndex \tcp*[f]{XXX}(a)}
\Output{CSemCompositeIndex \tcp*[f]{YYY}(b)}
\ForEach{entry $\in$ SemCompositeIndex \tcp*[f]{XXX}}{
CSemCompositeIndex.put(entry.compositeKey, compressMatchCounter(entry.matchCounter)) \tcp*[f]{ZZZ}}
\caption{Compress}
\label{alg:compress}
\end{algorithm}
\end{document}
I'm new here. Let me know if I need to put more details!
Thank you so much for your attention and participation.
Happy new year to all of us!
To place a comment after the condition of the for loop, you can use \ForEach(\tcp*[f]{XXX}){...}{....}
\documentclass{article}
\usepackage{mdframed}
\usepackage[noend,linesnumbered,ruled,vlined]{algorithm2e}
\setlength{\algomargin}{15pt} %#Werner Solution for left margin
\makeatletter
%Remove right hand margin in algorithm
\patchcmd{\#algocf#start}% <cmd>
{-1.5em}% <search>
{0pt}% <replace>
{}{}% <success><failure>
\makeatother
\begin{document}
\begin{algorithm}
\SetAlgoLined
\SetKwInOut{Input}{input}
\SetKwInOut{Output}{output}
\Input{SemCompositeIndex \tcp*[f]{XXX}(a)}
\Output{CSemCompositeIndex \tcp*[f]{YYY}(b)}
\ForEach(\tcp*[f]{XXX}){entry $\in$ SemCompositeIndex }{
CSemCompositeIndex.put(entry.compositeKey, compressMatchCounter(entry.matchCounter)) \tcp*[f]{ZZZ}}
\caption{Compress}
\label{alg:compress}
\end{algorithm}
\end{document}
This is the default layout of an algorithm when using algorithm2e:
\documentclass{article}
\usepackage[noend,linesnumbered,ruled,vlined]{algorithm2e}
%\setlength{\algomargin}{0pt}
\begin{document}
\begin{algorithm}[H]
\SetAlgoLined
\KwData{this text}
\KwResult{how to write algorithm with \LaTeX2e }
initialization\;
\While{not at end of this document}{
read current\;
\eIf{understand}{
go to next section\;
current section becomes this one\;
}{
go back to the beginning of current section\;
}
}
\caption{How to write algorithms}
\end{algorithm}
\end{document}
You can use \setlength{\algomargin}{<len>} to change the margins. For example, this is what \setlength{\algomargin}{0pt} looks like:
The default (first image above) is \leftskip + \parindent.
Sorry for bad english
I have a problem with my Latex code (I'm not very good in coding). I want an output like this:
Part I
Chapter I
Chapter II
Part II
Chapter I
Chapter II
I also wanted to create a box around my title of the parts , this doesn't work like I wanted, so I used \part* and \addcontentsline{toc}{part}{PART I} (to write the line in the table of content).So the code is:
\fbox{\begin{minipage}{\linewidth}
\part*{\begin{center}
PART I
\end{center}}
\end{minipage}}
\addcontentsline{toc}{part}{PART I}
\vspace{0.7cm}
I saw this methode to reset the counter of the chapters:
\makeatletter
\#addtoreset{chapter}{part}
\makeatother
but this doesn't work, I think it's due to use of \part*.
Has anyone an idea? THNANKS!
Hope the example bellow can help:)
\documentclass[a4paper,11pt,]{report}
\usepackage{hyperref}
\begin{document}
\tableofcontents
\newpage
\fbox{\begin{minipage}{\linewidth}
\part*{\begin{center}
INTRODUCTION
\end{center}}
\end{minipage}}
\addcontentsline{toc}{part}{INTRODUCTION}
\vspace{0.7cm}
\chapter{Hello}
\section{my}
\newpage
\fbox{\begin{minipage}{\linewidth}
\part*{\begin{center}
CONCLUSION
\end{center}}
\end{minipage}}
\addcontentsline{toc}{part}{CONCLUSION}
\vspace{0.7cm}
\chapter{name}
\section{is}
\end{document}
As you already know the solution for \part, how about simply using this and make it look like \part*? This has the advantage that you could add the boxes around the part titles automatically.
\documentclass[a4paper,11pt,]{report}
\usepackage[newparttoc]{titlesec}
\titleformat{\part}[frame]
{\normalfont}
{}
{8pt}
{\Large\bfseries\filcenter}
\usepackage{titletoc}
\titlecontents{part}[0em]
{\vspace{2em}\large\bfseries\sffamily\relax}
{\contentslabel[\relax]{0em}}{}{\hfill\contentspage}
\usepackage{hyperref}
\makeatletter
\#addtoreset{chapter}{part}
\makeatother
\begin{document}
\tableofcontents
\part{INTRODUCTION}
\chapter{Hello}
\section{my}
\part{CONCLUSION}
\chapter{name}
\section{is}
\end{document}
I want to cite LaTeX code into my document but how do I embed the keywords "\begin{lstlisting}" and "\end{lstlisting}" correctly?
CODE BELOW DOES NOT WORK (obviously):
\lstset{language=TeX, basicstyle=\footnotesize, numbers=left, numberstyle=\tiny, frame=single}
\begin{lstlisting}
\begin{lstlisting} % this is code
place your source code here % this is code
\end{lstlisting} % this is code
\end{lstlisting}
Do you have \usepackage{listings} in your preamble? If so, it should work. TeX is a supported language.
Here's a minimal example:
\documentclass{article}
\usepackage{listings}
\begin{document}
This is a StackOverflow test file.\\
To use \texttt{lstlisting}, include this in the preamble:
\begin{lstlisting}
\usepackage{listings}
\end{lstlisting}
Hope that helped :)
\end{document}
which compiles to
EDIT
To quote commands from the listings package (actually, only for \end{lstlisting}), escape to latex to print the \ character and you're all set. In the following, I've defined # as the escape character and everything within two # symbols is typeset in LaTeX. So here, I've input the \ using LaTeX and the rest within lstlisting and the \end{...} sequence is not interpreted as a closing the environment.
\documentclass{article}
\usepackage{listings}
\begin{document}
This is a StackOverflow test file.\\
Use escape characters to escape to \LaTeX
\lstset{escapechar=\#}
\begin{lstlisting}
\begin{lstlisting}
some code here
#\textbackslash#end{lstlisting}
\end{lstlisting}
Hope that helped :)
\end{document}
The output is
can you use a verbatim block?
\begin{verbatim}
\begin{lstlisting} % this is code
place your source code here % this is code
\end{lstlisting} % this is code
\end{verbatim}
You can use
\lstnewenvironment{OtherListing}
{}
{}
to create a new envirnment that behaves just list lstlisting, and \end{lstlisting} should not be forbidden in it.
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}