### Complete Working Example: Dijkstra's Algorithm Source: https://context7.com/chrmatt/algpseudocodex/llms.txt A full LaTeX document demonstrating Dijkstra's shortest path algorithm using algpseudocodex. It includes custom indent line styles, comments, and highlighted relaxation steps. ```latex \documentclass{article} \usepackage{algorithm} \usepackage{algpseudocodex} \usepackage{xcolor} % Custom indent line style \tikzset{algpxIndentLine/.style={draw=gray!50, very thin}} \begin{document} \begin{algorithm} \caption{Dijkstra's Shortest Path} \begin{algorithmic}[1] \Require Graph $G=(V,E,w)$ with non-negative weights, source $s \in V$ \Ensure $dist[v]$ = shortest distance from $s$ to every $v \in V$ \ForAll{$v \in V$} \State $dist[v] \gets \infty$ \EndFor \State $dist[s] \gets 0$ \State $Q \gets V$ \Comment{Priority queue of unvisited nodes} \While{$Q \neq \emptyset$} \State $u \gets$ \Call{ExtractMin}{$Q$} \LComment{Relax all edges leaving $u$. Only neighbors still in $Q$ can improve their tentative distance.} \ForAll{neighbor $v$ of $u$ with $v \in Q$} \State $alt \gets dist[u] + w(u,v)$ \If{$alt < dist[v]$} \BeginBox[fill=yellow!40] \State $dist[v] \gets alt$ \Comment{Relaxation step} \State $prev[v] \gets u$ \EndBox \EndIf \EndFor \EndWhile \State \Return $dist$ \end{algorithmic} \end{algorithm} \begin{algorithm} \caption{Linked List class example} \begin{algorithmic} \Class{Stack} \Properties \State \textbf{array} $data$ \State \textbf{int} $top \gets 0$ \EndProperties \Methods \Procedure{Push}{$x$} \State $data[top] \gets x$; $top \gets top + 1$ \EndProcedure \Function{Pop}{} \State $top \gets top - 1$ \State \Return $data[top]$ \EndFunction \EndMethods \EndClass \end{algorithmic} \end{algorithm} \end{document} ``` -------------------------------- ### State and Statex for Numbered/Unnumbered Lines Source: https://context7.com/chrmatt/algpseudocodex/llms.txt `\State` starts a new numbered statement line, while `\Statex` continues on a new line without incrementing the line counter. `\Return` and `\Output` are inline keywords used with `\State`. ```latex \begin{algorithmic}[1] \State $x \gets y^2$ \Statex \quad (this continues without a new line number) \State \Return $x + 1$ \State \Output the result is $x$ \end{algorithmic} ``` -------------------------------- ### Rename Algorithmic Keywords with \algrenewcommand Source: https://context7.com/chrmatt/algpseudocodex/llms.txt Rename keywords in algorithmic environments using \algrenewcommand, similar to the mechanism in algorithmicx. This example shows how to use French keywords and C-style comment delimiters. ```latex % Use French keywords \algrenewcommand\algorithmicif{\textbf{si}} \algrenewcommand\algorithmicthen{\textbf{alors}} \algrenewcommand\algorithmicelse{\textbf{sinon}} \algrenewcommand\algorithmicend{\textbf{fin}} \algrenewcommand\algorithmicwhile{\textbf{tant que}} \algrenewcommand\algorithmicdo{\textbf{faire}} \algrenewcommand\algorithmicfor{\textbf{pour}} \algrenewcommand\algorithmicfunction{\textbf{fonction}} \algrenewcommand\algorithmicreturn{\textbf{retourner}} % C-style comment delimiters \usepackage[beginComment={//~}, beginLComment={/*~}, endLComment={~*}]{algpseudocodex} ``` -------------------------------- ### Customizing Indent Guide Lines in Algpseudocodex Source: https://context7.com/chrmatt/algpseudocodex/llms.txt Allows customization of the appearance of indent guide lines using TikZ styles. The default style is `draw=gray, very thin`. ```latex % Blue dashed indent lines \tikzset{algpxIndentLine/.style={draw=blue, dashed}} % Thin red dotted lines \tikzset{algpxIndentLine/.style={draw=red!60, dotted, very thin}} \begin{algorithmic}[1] \If{$x > 0$} \State $x \gets x - 1$ \While{$x > 10$} \State $x \gets x / 2$ \EndWhile \EndIf \end{algorithmic} ``` -------------------------------- ### Customize Default Box Style with TikZ Source: https://context7.com/chrmatt/algpseudocodex/llms.txt Control the default appearance of boxes created by \BeginBox and \BoxedString using the algpxDefaultBox key. This example sets a blue filled background for unstyled boxes. ```latex % Make all unstyled boxes use a blue filled background \tikzset{algpxDefaultBox/.style={draw=blue, fill=blue!10}} \begin{algorithmic} \BeginBox % uses the new default style \State this line has a blue box \EndBox \State \BoxedString{inline box} % also uses the new default style \end{algorithmic} ``` -------------------------------- ### Preconditions and Postconditions with \Require and \Ensure Source: https://context7.com/chrmatt/algpseudocodex/llms.txt Specify preconditions and postconditions. `spaceRequire=true` (default) inserts a vertical gap before `\Require` not on the first line, aiding documentation of multiple input cases. ```latex \begin{algorithmic}[1] \Require $x \in \{0, 1\}$ \Ensure $y \in \{1, 2\}$ \State $y \gets x + 1$ \State \Return $y$ % % Second behavior block (spaceRequire adds vertical space above this \Require): \Require $x \in \{1, 2\}$ \Ensure $y \in \{0, 1\}$ \State \Return $x - 1$ \end{algorithmic} ``` -------------------------------- ### While Loop in Algpseudocodex Source: https://context7.com/chrmatt/algpseudocodex/llms.txt Illustrates a basic while loop structure. Ensure the loop condition eventually becomes false to avoid infinite loops. ```latex \begin{algorithmic}[1] \While{$n > 0$} \State $n \gets n - 1$ \EndWhile \end{algorithmic} ``` -------------------------------- ### Load Algpseudocodex Package Source: https://context7.com/chrmatt/algpseudocodex/llms.txt Load the package in the document preamble. Options can be passed to customize behavior like disabling end-keywords, changing comment styles, and controlling indent lines. ```latex % Minimal usage – all defaults apply \usepackage{algpseudocodex} ``` ```latex % With options: disable end-keywords, use non-italic blue comments \usepackage[noEnd=false, italicComments=false, commentColor=blue]{algpseudocodex} ``` ```latex % Full option syntax \usepackage[ noEnd=true, % suppress "end if", "end for", etc. (default: true) indLines=true, % draw indent guide lines (default: true) spaceRequire=true, % extra space before repeated \Require (default: true) italicComments=true, % italic comment text (default: true) rightComments=true, % right-justify \Comment (default: true) commentColor=gray, % color for all comments (default: gray) beginComment={$\triangleright$~}, % prefix for \Comment endComment={}, % suffix for \Comment beginLComment={$\triangleright$~}, % prefix for \LComment endLComment={~$\triangleleft$} ]{algpseudocodex} ``` -------------------------------- ### Algorithmic Environment with Line Numbering Source: https://context7.com/chrmatt/algpseudocodex/llms.txt The main environment for pseudocode. The optional integer argument to \begin{algorithmic} controls line numbering, where n > 0 prints a number on every n-th line. ```latex \begin{algorithm} \caption{Binary Search} \begin{algorithmic}[1] % number every line \Require sorted array $A$, target value $v$ \Ensure index $i$ such that $A[i] = v$, or $-1$ \State $lo \gets 1$; $hi \gets |A|$ \While{$lo \leq hi$} \State $mid \gets \lfloor (lo + hi) / 2 \rfloor$ \If{$A[mid] = v$} \State \Return $mid$ \ElsIf{$A[mid] < v$} \State $lo \gets mid + 1$ \Else \State $hi \gets mid - 1$ \EndIf \EndWhile \State \Return $-1$ \end{algorithmic} \end{algorithm} ``` -------------------------------- ### For Loop in Algpseudocodex Source: https://context7.com/chrmatt/algpseudocodex/llms.txt Demonstrates a for loop iterating from 1 to n. This is useful for iterating over a known range. ```latex \begin{algorithmic}[1] \For{$i = 1, \dots, n$} \State $sum \gets sum + A[i]$ \EndFor \end{algorithmic} ``` -------------------------------- ### For-All Loop in Algpseudocodex Source: https://context7.com/chrmatt/algpseudocodex/llms.txt Shows a for-all loop iterating over elements in a set V. Use this when processing each element of a collection. ```latex \begin{algorithmic}[1] \ForAll{$v \in V$} \State \Call{Visit}{$v$} \EndFor \end{algorithmic} ``` -------------------------------- ### Procedure/Function Call with \Call Source: https://context7.com/chrmatt/algpseudocodex/llms.txt Typesets a procedure or function call with small-caps name formatting. It takes two arguments: the procedure name and the argument list. ```latex \begin{algorithmic}[1] \State $result \gets$ \Call{BinarySearch}{$A, v$} \State \Call{Sort}{$A$} % no arguments: leave second arg empty \State \Call{Swap}{$A[i], A[j]$} \end{algorithmic} ``` -------------------------------- ### Inline and Block Comments in Algpseudocodex Source: https://context7.com/chrmatt/algpseudocodex/llms.txt Adds comments to pseudocode. `\Comment` is for inline comments, while `\LComment` is for multi-line block comments. ```latex \begin{algorithmic}[1] \State $x \gets y^2$ \LComment{The next two steps increment both $x$ and $y$. This comment can span as many lines as needed.} \State $x \gets x + 1$ \Comment{Increment $x$.} \State $y \gets y + 1$ \Comment{Increment $y$.} \end{algorithmic} ``` -------------------------------- ### Structure and Class Definition in Algpseudocodex Source: https://context7.com/chrmatt/algpseudocodex/llms.txt Defines data structures and classes with properties and methods. Use `\Structure` for data aggregates and `\Class` for objects with behavior. ```latex \begin{algorithmic} \Structure{Node} \State \textbf{int} $value$ \State \textbf{Node} $next$ \EndStructure \Class{LinkedList} \Properties \State \textbf{Node} $head$ \State \textbf{int} $size$ \EndProperties \Methods \Procedure{Insert}{$v$} \State $n \gets$ \textbf{new} \Call{Node}{$v$} \State $n.next \gets head$; $head \gets n$ \State $size \gets size + 1$ \EndProcedure \Function{Find}{$v$} \State $cur \gets head$ \While{$cur \neq \textbf{nil}$} \If{$cur.value = v$} \State \Return $cur$ \EndIf \State $cur \gets cur.next$ \EndWhile \State \Return \textbf{nil} \EndFunction \EndMethods \EndClass \end{algorithmic} ``` -------------------------------- ### If-ElsIf-Else Statement in Algpseudocodex Source: https://context7.com/chrmatt/algpseudocodex/llms.txt Demonstrates a standard conditional structure with if, elsif, and else branches. Executes one block based on the conditions. ```latex \begin{algorithmic}[1] \If{$x > 0$} \State \Return positive \ElsIf{$x < 0$} \State \Return negative \Else \State \Return zero \EndIf \end{algorithmic} ``` -------------------------------- ### Procedure and Function Definition in Algpseudocodex Source: https://context7.com/chrmatt/algpseudocodex/llms.txt Defines named subroutines (procedures and functions) with parameter lists. Procedures do not return a value, while functions do. ```latex \begin{algorithmic}[1] \Function{Merge}{$A, lo, mid, hi$} \State $L \gets A[lo \dots mid]$ \State $R \gets A[mid+1 \dots hi]$ \State $i \gets 1$; $j \gets 1$; $k \gets lo$ \While{$i \leq |L|$ \textbf{and} $j \leq |R|$} \If{$L[i] \leq R[j]$} \State $A[k] \gets L[i]$; $i \gets i+1$ \Else \State $A[k] \gets R[j]$; $j \gets j+1$ \EndIf \State $k \gets k+1$ \EndWhile \State \Return $A$ \EndFunction \Procedure{MergeSort}{$A, lo, hi$} \If{$lo < hi$} \State $mid \gets \lfloor (lo+hi)/2 \rfloor$ \State \Call{MergeSort}{$A, lo, mid$} \State \Call{MergeSort}{$A, mid+1, hi$} \State \Call{Merge}{$A, lo, mid, hi$} \EndIf \EndProcedure \end{algorithmic} ``` -------------------------------- ### Box Drawing with \BeginBox / \EndBox in Algpseudocodex Source: https://context7.com/chrmatt/algpseudocodex/llms.txt Draws boxes around pseudocode lines, allowing for highlighting or grouping. Supports nested boxes and custom TikZ styles. ```latex \begin{algorithmic} \BeginBox % default style: draw (black border) \State original line 1 \BeginBox[fill=yellow] % nested box with yellow fill \State highlighted line 2 \State highlighted line 3 \EndBox \State original line 4 \EndBox \BeginBox[draw=red, dashed, thick] % custom TikZ style \State differently styled box \EndBox \end{algorithmic} ``` -------------------------------- ### Infinite Loop in Algpseudocodex Source: https://context7.com/chrmatt/algpseudocodex/llms.txt An infinite loop structure that includes a conditional return. Ensure a break condition exists to exit the loop. ```latex \begin{algorithmic}[1] \Loop \State \Call{Process}{} \If{done} \State \Return \EndIf \EndLoop \end{algorithmic} ``` -------------------------------- ### Repeat-Until Loop in Algpseudocodex Source: https://context7.com/chrmatt/algpseudocodex/llms.txt A repeat-until loop that executes the body at least once before checking the condition. The loop continues until the condition is met. ```latex \begin{algorithmic}[1] \Repeat \State $x \gets x / 2$ \Until{$x < \epsilon$} \end{algorithmic} ``` -------------------------------- ### Boxed Strings with \BoxedString in Algpseudocodex Source: https://context7.com/chrmatt/algpseudocodex/llms.txt Draws a box around inline strings or code elements within a line. Accepts TikZ styles for customization. ```latex \begin{algorithmic}[1] \State swap \BoxedString[fill=cyan]{$A[i]$} and \BoxedString[fill=orange]{$A[j]$} \State the \BoxedString[draw=red,thick]{changed part} is highlighted \end{algorithmic} ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.