I would like to use LaTeX to document my C++ code, since I am able to typeset mathematics easily, as well as create diagrams using TikZ. I made a minimal LaTeX template for testing, but I have already encountered some problems that I would like to fix:
- Spaces are not generated at the end of each line of text in the escaped LaTeX code (if you look at the rendered output below, you should see some words stuck together).
- I would like environments in the escaped LaTeX code to stay within the shaded boundary, with line numbers to the left of each line (as it is with the rest of the code). In particular, I want to enforce a 1:1 correspondence between the number of a line of source code and its rendered output in the PDF.
Can you please suggest a fix for these problems? Ideally, I would also like to be able to include TikZ pictures in the documentation, so that the diagram takes up exactly as many lines of code as the length of the escaped LaTeX code that I use to create it. But if this is too difficult to achieve, then I'm fine without it.
Thank you very much for your time!
The Rendered Output

The LaTeX Code
\documentclass[11pt, a4paper]{article}
\usepackage[T1]{fontenc}
\usepackage[top=0.4in,bottom=0in,left=0.7in,right=0.7in]{geometry}
\usepackage{listings}
\usepackage{mdframed}
\usepackage[usenames,dvipsnames]{xcolor}
\pagestyle{empty}
\lstset{
language=C++,
basicstyle=\sffamily,
commentstyle=\color{white!40!black},
escapechar=`,
escapebegin=\color{white!40!black},
escapeend={},
keywordstyle=\bfseries\color{Periwinkle},
numbers=left,
numberstyle=\sffamily\tiny\color{gray},
showstringspaces=false
}
\begin{document}
\begin{mdframed}[backgroundcolor=yellow!20,linewidth=0pt,
innertopmargin=0pt,innerbottommargin=0pt]
{\footnotesize
\lstinputlisting{test.hpp}
}
\end{mdframed}
\end{document}
The Sample C++ Header
/*
** File Name: test.hpp
** Date: 04/25/2012
*/
#ifndef TEST_HPP_INCLUDED__
#define TEST_HPP_INCLUDED__
/*
** Template Parameters:
**
`\begin{enumerate}
\item[Schedule] A forward iterator that traverses the edge ordering that
ought to be used for each iteration.
\item[EdgeToFactorMap] A map of edges to factors. We can always transform a
graph such that the node potentials are subsumed into the edge potentials, so
this constraint does not result in a loss of generality.
\item[MessageFunc] A function used to compute the message
$\delta_{i \rightarrow j}$. It should accept as parameters the following:
\begin{itemize}
\item A const reference to a factor along $E_{ij}$.
\item A reference to the target factor to be written to.
\item The starting and ending iterators to a container of factors
representing the messages received by the sending clique, $C_{i}$.
\item The starting and ending iterators to a container of factors
representing the messages received by the receiving clique, $C_{j}$.
\end{itemize}
\item[Allocator] The allocator that ought to be used to obtain temporary
memory for the algorithm.
\end{enumerate}`
**
*/
template <
class Schedule,
class EdgeToFactorMap,
class MessageFunc,
class Allocator
>
#endif
