# How to have one code to be typeset both as final and as source code [duplicate]

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.

## marked as duplicate by Werner, Phelype Oleinik, Troy, user121799, SebastianoFeb 6 '18 at 20:41

• You're probably looking for Any way to show LaTeX example code and execute it? – Werner Feb 6 '18 at 18:00
• You're all right: my question, as is, is a duplicate! I ommited, however, I needed the algorithm and its source to be placed independently in parts of the document. Sorry for that, but I thought my question was already too long. I came with a solution mixing Oleinik's and moewe's answers. It's not so elegant, but solved my problem. I will edit the question to show my solution. Thanks to all. – Jander Feb 8 '18 at 17:10

You could use the filecontents package:

\documentclass{article}
\usepackage[utf8]{inputenc}
\usepackage{algpseudocode}

\usepackage{fancyvrb}% To use \VerbatimInput
\usepackage{filecontents}

\begin{filecontents}{exampleone.alg}
\If{$a > b$}
\State $c \gets a$
\Else
\State $c \gets b$
\EndIf
\end{filecontents}

\begin{document}

The code

\VerbatimInput[firstline=5]{exampleone.alg}

is typeset as

\begin{algorithmic}
\input{exampleone.alg}
\end{algorithmic}
\end{document}

• It was an earlier attempt of mine, but I'll have hundreds of examples and have a separate file for each one seams not practical. Appreciate the thought anyway. I'll take a look if its possible do let Latex write the separate files transparently. – Jander Feb 6 '18 at 18:15
• No problem. See Werner's comments. I thought it might help since eventually you would have to type the algorithms somewhere. – Phelype Oleinik Feb 6 '18 at 18:18

With the tcolorbox package:

\documentclass{article}

\usepackage[T1]{fontenc}
\usepackage[utf8]{inputenc}

\usepackage[most]{tcolorbox}
\usepackage{algpseudocode}

\tcbset{%
sidebyside,
before lower={\begin{algorithmic}},
after lower={\end{algorithmic}},
}

\begin{document}

\begin{tcblisting}{title={test}}
\If{$a > b$}
\State $c \gets a$
\Else
\State $c \gets b$
\EndIf
\end{tcblisting}

\end{document}


• @moewe: thanks for pointing to tcolorbox, as I didn't know it was able to show the source code. I came, however, with a different solution using tcolorbox to save the code and fancyvrb to input it back. – Jander Feb 8 '18 at 17:22
• @Jander All credits go to @samcarter. I just fixed a typo. Use whatever floats your boat. I happen to think this tcolorbox solution is very elegant, but I can't do more than up-vote the answer (and trying to remember it should I need something similar). – moewe Feb 8 '18 at 18:58
• Sorry @samcarter... I misread the author! :-) – Jander Feb 19 '18 at 12:47
• @Jander Don't worry :) – user36296 Feb 19 '18 at 12:48