Coder Social home page Coder Social logo

algpseudocodex's Introduction

algpseudocodex

Overview

This package allows typesetting pseudocode in LaTeX. It is based on algpseudocode from the algorithmicx package and uses the same syntax, but adds several new features and improvements. Notable features include customizable indent guide lines and the ability to draw boxes around parts of the code for highlighting differences. This package also has better support for long code lines spanning several lines and improved comments.

The package documentation can be found in the file algpseudocodex.tex.

Author and Contact

The package is authored and maintained by Christian Matt. For bug reports and other issues please visit the official repository at https://github.com/chrmatt/algpseudocodex.

License

This material is subject to the LaTeX Project Public License (LPPL), version 1.3c.

algpseudocodex's People

Contributors

chrmatt avatar note286 avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

algpseudocodex's Issues

Support braces {} to wrap the state bodies in Blocks

In some earlier packages such as algorithmic, the statement in blocks could be wrapped in braces, like:

\FOR{\(k=1,\ldots,n\)}
{
    \STATE{Some statement}
}
\ENDFOR

Such braces {} are very useful for the document formatter (such as the Latex extension in VSCode) to get pretty indents.
However this will cause error for algpseudocodex package.

We hope this could be enhanced♥!

Unexpected end of Indent line for if elsif else blocks

Issue

When the if/elsif/else blocks contain another block, then the indent line finishes before the end of the if chain.

What is expected

The indent line of a if-elsif-chain should not finish after the end of the \EndIf

MWE

\documentclass[a4paper,10pt]{article}
\usepackage[utf8]{inputenc}
\usepackage{algpseudocodex}
\usepackage{algorithm}

\begin{document}

\begin{algorithm}[H]
    \begin{algorithmic}[1]
        \If{test}
            \Comment{indent line end before else: wrong.}
            \For{\(a \in A\)}
                \State{for body}
            \EndFor{}
        \ElsIf{elseif}\Comment{indent line end after else: correct.}
            \State{state in elsif}
        \Else{}
            \State{state in else}
        \EndIf{}
        \If{test}\Comment{indent line end after elsif: correct.}
            \State{state in if}
        \ElsIf{elseif}
            \Comment{indent line end before else: wrong.}
            \For{\(a \in A\)}
                \State{for body}
            \EndFor{}
        \Else{}
            \State{state in else}
        \EndIf{}
        \LComment{all correct.}
        \If{test}
            \State{state in if}
        \ElsIf{elseif}
            \State{state in elsif}
        \Else{}
            \State{state in else}
        \EndIf{}
    \end{algorithmic}
\end{algorithm}

\end{document}

main.pdf

Configuration

 *File List*
 article.cls    2021/10/04 v1.4n Standard LaTeX document class
  size10.clo    2021/10/04 v1.4n Standard LaTeX file (size option)
algpseudocodex.sty    2021-12-05 v1.0.1 pseudocode typesetting
kvoptions.sty    2022-06-15 v3.15 Key value format for package options (HO)
  keyval.sty    2022/05/29 v1.15 key=value parser (DPC)
 ltxcmds.sty    2020-05-10 v1.25 LaTeX kernel commands for general use (HO)
kvsetkeys.sty    2019/12/15 v1.18 Key value parser (HO)
algorithmicx.sty    2005/04/27 v1.2 Algorithmicx
  ifthen.sty    2022/04/13 v1.1d Standard LaTeX ifthen package (DPC)
etoolbox.sty    2020/10/05 v2.5k e-TeX tools for LaTeX (JAW)
fifo-stack.sty    2014/03/24 v1.0 Multi-FIFO/stack system
varwidth.sty    2009/03/30 ver 0.92;  Variable-width minipages
   tabto.sty    2018/12/28  v 1.4  Another tabbing mechanism
    tikz.sty    2021/05/15 v3.1.9a (3.1.9a)
     pgf.sty    2021/05/15 v3.1.9a (3.1.9a)
  pgfrcs.sty    2021/05/15 v3.1.9a (3.1.9a)
pgfrcs.code.tex
 pgfcore.sty    2021/05/15 v3.1.9a (3.1.9a)
graphicx.sty    2021/09/16 v1.2d Enhanced LaTeX Graphics (DPC,SPQR)
graphics.sty    2022/03/10 v1.4e Standard LaTeX Graphics (DPC,SPQR)
    trig.sty    2021/08/11 v1.11 sin cos tan (DPC)
graphics.cfg    2016/06/04 v1.11 sample graphics configuration
  luatex.def    2021/06/01 v1.2c Graphics/color driver for luatex
  pgfsys.sty    2021/05/15 v3.1.9a (3.1.9a)
pgfsys.code.tex
pgfsyssoftpath.code.tex    2021/05/15 v3.1.9a (3.1.9a)
pgfsysprotocol.code.tex    2021/05/15 v3.1.9a (3.1.9a)
  xcolor.sty    2022/06/12 v2.14 LaTeX color extensions (UK)
   color.cfg    2016/01/02 v1.6 sample color configuration
mathcolor.ltx
pgfcore.code.tex
pgfcomp-version-0-65.sty    2021/05/15 v3.1.9a (3.1.9a)
pgfcomp-version-1-18.sty    2021/05/15 v3.1.9a (3.1.9a)
  pgffor.sty    2021/05/15 v3.1.9a (3.1.9a)
 pgfkeys.sty    
pgfkeys.code.tex
 pgfmath.sty    
pgfmath.code.tex
pgffor.code.tex
tikz.code.tex
   expl3.sty    2022-08-30 L3 programming layer (loader) 
l3backend-luatex.def    2022-08-30 L3 backend support: PDF output (LuaTeX)
l3keys2e.sty    2022-06-22 LaTeX2e option processing using LaTeX3 keys
  xparse.sty    2022-06-22 L3 Experimental document command parser
algorithm.sty    2009/08/24 v0.1 Document Style `algorithm' - floating environment
   float.sty    2001/11/08 v1.3d Float enhancements (AL)
  ts1cmr.fd    2019/12/16 v2.5j Standard LaTeX font definitions
supp-pdf.mkii
epstopdf-base.sty    2020-01-24 v2.11 Base part for package epstopdf
epstopdf-sys.cfg    2010/07/13 v1.3 Configuration of (r)epstopdf for TeX Live
 ***********

LComment without line numbering

Hi there!

I'm wondering if there is a way to create an LComment without the line numbering. I'm trying to write down an algorithm, where the individual steps can be sectioned into a few different groups. Basically, I'm trying to (mis-) use LComments to create small headings for different parts of the algorithm, and I would like to do that without line numbers.

Also, I would like to thank you for creating this package.

Update: Due to what appears to be a bug in algorithmicx, I ended up making my own commands for these headings. One command for the first heading directly after a \Procedure, and one for headings after \State has been used:

\algnewcommand{\AlgHeading}[1]{\Statex \textcolor{gray}{\textit{#1}}}
\algnewcommand{\AlgHeadingInitial}[1]{\Statex \hspace{\algorithmicindent}\textcolor{gray}{\textit{#1}}}

problem using algpseudocodex when breqn is loaded

Fyi;

I found a problem using algpseudocodex package with breqn package. I asked about at lualatex-compile-error-when-using-an-option-with-algpseudocodex-package-with-bre

May be there is a way to fix this?

Here is the content of the question from above

On latest TL 2023, this MWE below gives error

\documentclass[12pt]{article}

\usepackage{amsmath} 
\usepackage{breqn}

\usepackage[noEnd=false]{algpseudocodex}
\usepackage{algorithm}

\begin{document}
\begin{algorithm}
\begin{algorithmic}[0]
\Procedure{A}{x}
\State do something
\EndProcedure
\end{algorithmic}
\end{algorithm}

\end{document}

compiling with lualatex gives

/usr/local/texlive/2023/texmf-dist/tex/latex/latexconfig/epstopdf-sys.cfg))

! LaTeX Error: \begin{varwidth} on input line 14 ended by \end{list}.

See the LaTeX manual or LaTeX Companion for explanation.
Type  H <return>  for immediate help.
 ...                                              
                                                  
l.15 \end{algorithmic}
                    
? 

By changing the line \usepackage[noEnd=false]{algpseudocodex} to \usepackage[noEnd=true]{algpseudocodex} now it compiles with no error!

This only happens when breqn package is loaded. If I remove breqn package then both options compile with no error. But I have to use breqn package.

Is there a way to make it work with noEnd=false option and also use breqn package?

Custom algorithm block definitions from algorithmcx do not render properly

Hi!

I'm trying to write an algorithm with custom switch and case statements and in algorithmicx I'm able to write the following using \algdef:

\algnewcommand\algorithmicswitch{\textbf{switch}}
\algnewcommand\algorithmiccase{\textbf{case}}
\algdef{SE}[SWITCH]{Switch}{EndSwitch}[1]{\algorithmicswitch\ #1\ \algorithmicdo}{\algorithmicend\ \algorithmicswitch}%
\algdef{SE}[CASE]{Case}{EndCase}[1]{\algorithmiccase\ #1}{\algorithmicend\ \algorithmiccase}%
\algtext*{EndSwitch}%
\algtext*{EndCase}%

However, when I do so the following code gets rendered as so:

\documentclass[letterpaper,10pt]{article}

\usepackage{algpseudocodex}
\usepackage{algorithm}
\usepackage{tikz}

\algnewcommand\algorithmicswitch{\textbf{switch}}%
\algnewcommand\algorithmiccase{\textbf{case}}%
\algdef{SE}[SWITCH]{Switch}{EndSwitch}[1]{\algorithmicswitch\ #1\ \algorithmicdo}{\algorithmicend\ \algorithmicswitch}%
\algdef{SE}[CASE]{Case}{EndCase}[1]{\algorithmiccase\ #1}{\algorithmicend\ \algorithmiccase}%
\algtext*{EndSwitch}%
\algtext*{EndCase}%

\begin{document}
\begin{algorithm}[H]
  \caption{Test Example}
  \begin{algorithmic}[1]
    \Function{Foo}{x}
    \While{x != 0}
    \State $x = x-1$
    \Switch{x}
    \Case{0}
    \EndCase
    \Case{1}
    \EndCase
    \EndSwitch
    \EndWhile
    \EndFunction
  \end{algorithmic}
\end{algorithm}
\end{document}

2021-06-29-165243_373x234_scrot

Additionally, the document does not compile if I have \State ... in between the the case statements or the switch statements.

Do you have any workarounds for this? Thank you in advance! I'm switching to your package because highlighting code seems to be so much easier! :)

Compilation errors when using \includeonly

Hi !

I have a compilation error when using algorithmic environment when using \includeonly{...} to compile my chapters separately. I'm using an up-to-date texlive install with algpseudocodex v1.1.0.

Here is a MWE showing the issue :

\documentclass{scrreprt}

\usepackage{algpseudocodex}

\includeonly{mwe2.tex}

\begin{document}
\include{mwe1.tex}
\include{mwe2.tex}
\end{document}

And mwe1/2.tex contain :

\begin{algorithmic}[1]
  \Function{f}{$G$}
    \State $G$
  \EndFunction
\end{algorithmic}

The first error is :

! LaTeX Error: No counter 'algpx@indentEndPage-0' defined.

Deleting the .aux files removes the error. According to some colleagues, there was no error in the previous version of algpseudocodex.

Unevenly spaced line numbers for empty line

The following example

\documentclass{scrartcl}

\usepackage{algpseudocodex}

\begin{document}
	\begin{algorithmic}[1]
		\State First line
		\State
		\State Third line
	\end{algorithmic}
\end{document}

produces this output using TexLive 2022:
image

As you can see, the line number for the empty line is off. If some content is added to the second line, then the line numbers are all uniformly spaces as one would expect.

When there are multiple lines in such as For or If head, the indent guide lines should starts from the bottom of head.

\documentclass{article}
\usepackage{algpseudocodex}
\begin{document}
\begin{algorithmic}[1]
\For{
Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod
tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam,
quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo
consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse
cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non
proident, sunt in culpa qui officia deserunt mollit anim id est laborum.
}
\State tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam,
\State quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo
\State consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse
consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse
\EndFor
\end{algorithmic}
\end{document}

image

I checked the source code and found that multiple lines were not processed. It should start drawing from the bottom ② instead of the top ①.

Indent guide lines extended over pagebreak

It would be cool if the indent guide lines would respect pagebreaks. I documented this here: [https://tex.stackexchange.com/questions/620301/problem-with-page-break-and-indent-guide-lines-of-package-algpseudocodex]

I found this, which shows what behaviour I have in mind:[https://texwelt.de/fragen/23135/pagebreak-mit-einschrankungen]

Line spacing is broken

Since the latest update to the TexLive and MikTeX distributions (as of 2021-06-06) the spacing between the lines in algpseudocodex is off, and as a result, the indent lines are also misplaced.
Consider the following snippet:

\documentclass[10pt,a4paper]{book}

\usepackage{algorithm}
\usepackage{algpseudocodex}

\begin{document}

\begin{algorithm}
  \caption{Example Algorithm}
  \label{alg:example}
  \begin{algorithmic}[1]
    \Function{foo}{x,y}
    \While{\(x \ne y\)}
    \State \(x\gets x+1\) \Comment{Hello there}
    \EndWhile
    \State \Return \(x\)
    \EndFunction
  \end{algorithmic}
\end{algorithm}

\end{document}

Since the update, it renders as follows:

Screenshot 2021-06-07 124125

How to adjust lineskip in algorithmic?

I need to modify the lineskip value in a certain algorithmic environment so that it applies uniformly throughout the environment. It works in the algpseudocode package, but not in the algpseudocodex package. In the algpseudocodex package, I have to manually set it after each \State, which is very inconvenient. I have roughly read the source code, but I haven't found the reason. It seems like \State is resetting some values.

\documentclass{article}
% \usepackage{algpseudocodex}
\usepackage{algpseudocode}
\begin{document}
\begin{algorithmic}[1]
\addtolength{\lineskip}{10pt}
\Require
Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod
$\sum\limits_i^1$
tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam,
\Ensure
quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo
$\sum\limits_i^1$
consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse
\State
cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non
$\sum\limits_i^1$
proident, sunt in culpa qui officia deserunt mollit anim id est laborum.
\State
cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non
proident, sunt in culpa qui officia deserunt mollit anim id est laborum.
\State
cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non
$\sum\limits_i^1$
proident, sunt in culpa qui officia deserunt mollit anim id est laborum.
\end{algorithmic}
\end{document}

image


But if with algpseudocodex.

image


With algpseudocodex, must add after to \State.

\State \addtolength{\lineskip}{10pt}
cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non
$\sum\limits_i^1$
proident, sunt in culpa qui officia deserunt mollit anim id est laborum.

image

Not compatible with new math syntax `\( \)`

When using the \( \) math syntax, algpseudocodex breaks. So does algpseudocode, but then I have this patch:

%%% fix for usage of \( \) in Algpseudocode
% https://tex.stackexchange.com/questions/669665/use-in-algpseudocode
\newcommand{\algparenthesize}[1]{%
  \if\relax\detokenize{#1}\relax\else(#1)\fi
}
\algdef{SE}[PROCEDURE]{Procedure}{EndProcedure}[2]
 {\algorithmicprocedure\ \textproc{#1}\algparenthesize{#2}}
 {\algorithmicend\ \algorithmicprocedure}
\algdef{SE}[FUNCTION]{Function}{EndFunction}[2]
 {\algorithmicfunction\ \textproc{#1}\algparenthesize{#2}}
 {\algorithmicend\ \algorithmicfunction}
\algrenewcommand\Call[2]{\textproc{#1}\algparenthesize{#2}}
%%% end fix

Compile error with MacTeX 2024 on Mac OS Ventura

I have a MacBook running Monterey and another running Ventura, synched over iCloud. (So all my TeX files are identical.)
MacTeX 2024 runs perfectly on the Monterey machine. But when running MacTeX 2024 on Ventura with the identical project folder, \pageref does not display the pages in the PDF output. As far as I can tell, that is the only thing that fails in the Ventura compile. Everything else comes out fine on either machine. (Yes, I know you have to compile twice.) I’ve tried deleting the aux file on Ventura but that does not fix the issue. (The aux file DOES have all the pageref data on Ventura. But LaTeX doesn't read them; or at least does not print them in the output file.) Presumably this is a pathway problem that results from running in Ventura (so likely a MacOS file-system issue); but that's beyond my ken.

Issue with comments at the end of a block of code

Hello,

When adding comment to the end of a block of code, the comment is not right aligned as the other comments but rather it is shifted right by one indentation. I have attached an example showing comments wrongly placed on \EndIf and \Until using the parameter noEnd=false but the problem also happens when issuing comments on \Until even if noEnd=true.

Thank you for the support! There are several ways of fixing this manually or globally but I don't know which is the most idiomatic way nor which will not break something else…

\documentclass[a4paper,10pt]{article}
\usepackage[utf8]{inputenc}
\usepackage[noEnd=false]{algpseudocodex}
\usepackage{algorithm}

\begin{document}
\begin{algorithm}[H]
  
  \begin{algorithmic}[1]
    \If{test}\Comment{ok}
    \State \Comment{ok}
    \Else \Comment{still ok}
    \State aaaa\Comment{...}
    \EndIf  \Comment{shifted right}
    \Repeat  \Comment{fine}
    \Repeat  \Comment{fine}
    \State stmt \Comment{also fine}
    \Until{very very very very very long test}  \Comment{broken}
    \Until{short test} \Comment{broken}
    \State \Comment{fine again}
  \end{algorithmic}

\end{algorithm}
\end{document}

OUTPUT.pdf

BeginBox

When I use \BeginBox in algorithmic, I find that the spacing between the box inside and outside is particularly large. How can I fix it?
box

Labels don't work

\labels all show up as "1".

I'm not well versed in latex but could it be similar to this issue?

Love the package otherwise!

Indent line breaks when when using `\Statex` at the top of a new column

The indent lines seem to break when using the multicol package and \Statex near the top of a new column. Here's an example:

\documentclass{article}
\usepackage{algpseudocodex}
\usepackage{multicol}

\begin{document}
    \begin{multicols}{3}
        \begin{algorithmic}[1]
            \If{\(a > 0\)}
                \Statex \hspace{\algorithmicindent}no
                \State yes
                \State yes
            \EndIf
            \If{\(b > 0\)}
                \Statex \hspace{\algorithmicindent}no
                \State yes
                \State yes
            \EndIf
            \If{\(c > 0\)}
                \Statex \hspace{\algorithmicindent}no
                \State yes
                \State yes
            \EndIf
        \end{algorithmic}
    \end{multicols}
\end{document}

And here's the corresponding output:
image

The \hspace{\algorithmicindent} is added because \Statex does not produce the correct indentation as noted by @LeCyberDucky in #7. Note that the indent lines only break in the second and third column. They do not break when \Statex is replaced by \State or when there are fewer than two lines below \Statex in a column.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.