How to Write Pseudocode in LaTeX with Texmaker
At a glance
- 1. Choose a package
Use algorithm with algpseudocode for a numbered algorithm float and structured pseudocode commands.
- 2. Load the packages
Put the package commands in the preamble, before begin document.
- 3. Write the algorithm
Place pseudocode commands inside algorithm and algorithmic environments.
- 4. Compile the document
Compile with a LaTeX engine in Texmaker; the editor itself does not provide pseudocode commands.
Texmaker does not have a separate pseudocode mode: you write LaTeX source in the editor and use a package to format the algorithm. A flexible, widely used option is the algorithm package with algpseudocode, which provides a numbered floating algorithm and commands for pseudocode structures.
A minimal working example
Copy this complete document into a new .tex file in Texmaker and compile it with pdfLaTeX, LuaLaTeX, or XeLaTeX:
\documentclass{article}
\usepackage{algorithm}
\usepackage{algpseudocode}
\begin{document}
\begin{algorithm}
\caption{Find the maximum element}
\label{alg:maximum}
\begin{algorithmic}[1]
\Require Array $A[1\ldots n]$ with $n \geq 1$
\Ensure The maximum element of $A$
\State $m \gets A[1]$
\For{$i \gets 2$ to $n$}
\If{$A[i] > m$}
\State $m \gets A[i]$
\EndIf
\EndFor
\State \Return{$m$}
\end{algorithmic}
\end{algorithm}
Algorithm~\ref{alg:maximum} scans the array and keeps track of its largest value.
\end{document}
The algorithm package supplies the floating environment, caption, and label. The algpseudocode package supplies the algorithmic environment and commands such as \For, \If, and \State. The optional [1] after \begin{algorithmic} turns on line numbers; remove it if you do not want them. \Require and \Ensure typeset the input and output descriptions.
What the example does
The algorithm starts by assigning the first array value to $m$. It then checks each remaining element in turn. Whenever it finds a value larger than $m$, it replaces $m$ with that value. After the loop, $m$ is the maximum.
For example, if the input is , the initial value is 4. The loop changes it to 9 when it reaches the second element; the later values do not exceed 9. The algorithm therefore returns 9. The stated requirement matters because the initialization reads the first element.
Writing your own pseudocode
Use the commands provided by algpseudocode to mark the structure:
\Statestarts a statement.\If{condition}and\EndIfenclose a conditional; add\Elsewhen needed.\For{...}and\EndForenclose a loop.\While{...}and\EndWhileare also available.\Procedure{name}{arguments}and\EndProceduremark a procedure.\Returnmarks a return statement.
Put mathematical expressions in $...$, as in the example. For nested logic, indent the source for readability; the package formats the blocks as pseudocode. Use \caption to name the algorithm and \label if you want to refer to it elsewhere in the document.
Compile it in Texmaker
Save the file with a .tex extension, then run a LaTeX engine using Texmaker’s Quick Build or its separate compile command. The exact menu and shortcut depend on your Texmaker configuration. If you get an error such as File 'algorithm.sty' not found, the package is missing from the TeX distribution selected for compilation. Install the package in that distribution, then compile again. Installing it in one TeX setup will not necessarily make it available to another.
Choosing a different package
The example uses two packages together: algorithm handles the float, while algpseudocode handles the pseudocode commands. You may instead use algorithm2e, a separate package with its own commands and styling. Follow one package’s syntax consistently; the environments and commands are not interchangeable. Also check your document class or journal template, since it may already load an algorithm package or require a specific style.
Common mistakes
- Loading
algorithmicandalgpseudocodetogether: They are different interfaces and can conflict. For this example, loadalgorithmandalgpseudocodeonly. - Using pseudocode commands outside their environment: Commands such as
\Statebelong insidealgorithmic(or the corresponding environment from the package you chose). - Treating Texmaker as the compiler: Texmaker edits and runs LaTeX; the selected TeX distribution supplies the packages and performs compilation.
- Mixing package syntaxes: For instance,
algorithm2ehas its own environment and command conventions. Do not paste its commands into analgpseudocodeexample without adapting them.
References
Spotted an error? Send a correction. Read how we write and check articles in our editorial policy.