I'm very limited in writing Latex code and I could not find how to solve my problem searching in the forum ou googling around.
My problem: I have to write a document with many algorithms and show both the source code and the final typesetting.
This is a MWE of the result I want:
\documentclass{article}
\usepackage[utf8]{inputenc}
\usepackage{algpseudocode}
\begin{document}
The code
\begin{verbatim}
\If{$a > b$}
\State $c \gets a$
\Else
\State $c \gets b$
\EndIf
\end{verbatim}
is typeset as
\begin{algorithmic}
\If{$a > b$}
\State $c \gets a$
\Else
\State $c \gets b$
\EndIf
\end{algorithmic}
\end{document}
However, I don't want to replicate the code of each algorithm, as it makes hard to maintain and leads to inconsistencies.
I'd like to know if there is a way to define a code and render it as an algorithm and as a source code.
Here is an M(non)WE of what I'd like to have:
\documentclass{article}
\usepackage[utf8]{inputenc}
\usepackage{algpseudocode}
\begin{document}
\newsamplecode{\ExampleOne}{
\If{$a > b$}
\State $c \gets a$
\Else
\State $c \gets b$
\EndIf
}
The code
\begin{Verbatim}
\ExampleOneCode
\end{Verbatim}
is typeset as
\begin{algorithmic}
\ExampleOneAlg
\end{algorithmic}
\end{document}
So far, I'm trying to store the source code and make it available as two distinct macros. It's obviously not working and I'm running out of ideas. Here is what I got:
\newcommand{\newsamplecode}[2]{%
\expandafter\def\csname sc#1\endcsname{#2}
\expandafter\newcommand\csname sc#1verb\endcsname{\verb|#2|}
}
This successfully works:
\newsamplecode{codigodez}{
\State Input $s$
\If{$s$}
\State \textbf{say} \textit{ok}
\EndIf
}
\begin{algorithmic}
\sccodigodez
\end{algorithmic}
But I'm not able to render the source code in any way. I tried to use the xstring package to convert the argument to verbatim and/or to replace \ with \textbackslash. Until now, the internal commands (like \State) seems to be evaluated before any processing I try to apply.
If anyone could point me to a solution, I appreciate.
Edit with my solution
I created a new environment, which saved the code as is, and new commands to input the saved file in distinct context: to typeset as algorithm and to show it verbatim.
\documentclass{article}
\usepackage{algorithm} % numbered algorithms
\usepackage{algpseudocode} % algorithmic typeset
\usepackage{tcolorbox} % to save verbatim
\usepackage{fancyvrb} % to load verbatim preserving tabs
% environment and commands
\newenvironment{definecode}[1]{\begingroup\tcbverbatimwrite{\jobname_code_#1.tmp}}%
{\endtcbverbatimwrite\endgroup}
\newcommand{\algcode}[1]{\input{\jobname_code_#1.tmp}}
\newcommand{\sourcecode}[1]{\VerbatimInput{\jobname_code_#1.tmp}}
\begin{document}
% a code to be stored
\begin{definecode}{mycode}
\State $x \gets 1$
\While{$x < 10$}
\State $x \gets x-1$
\EndWhile
\end{definecode}
% using the stored code
See Alg~\ref{alg:my}.
\begin{algorithm}
\caption{My algorithm.}
\label{alg:my}
\begin{algorithmic}[1]
\algcode{mycode}
\end{algorithmic}
\end{algorithm}
And this is the source for Alg~\ref{alg:my}:
\sourcecode{mycode}
\end{document}
I opted to use the commands to detach an "algorithm name" in the code from its actual file name. I know that if I use the same name the file will be overwritten, but I can manage it manually for now.
