1

Inside of our document we wish to define a new environment for blocks of source code

\newenvironment{srcblock}%
  % Define the environment

Later, we will use the environment:

\begin{srcblock} % use the environment
  % source code here
\end{srcblock}

We seek the following behavior:

  • Each line in a src code block is numbered
  • The font used for a src code block is monospaced
  • A border is drawn around a code code block, much like \fbox does
  • Each code block "floats" to a position so that it will fit all on one page.
IdleCustard
  • 1,194

2 Answers2

3

Variation on my answer here: Seemingly trivial: verbatim, newenvironment, and colorbox. It requires a small modification to my verbatimbox package.

\documentclass{article}
\usepackage{xcolor,caption,lipsum}
\usepackage{verbatimbox}
\makeatletter
\newenvironment{cverbbox}[5][]{%
  \setcounter{VerbboxLineNo}{0}%
  \def\verbatim@processline{%
% THE FIRST #1 ACCOUNTS FOR NON-PRINTING COMMANDS; THE SECOND #1 IS FOR
% PRINTED OPTIONAL MATERIAL
    {\addtocounter{VerbboxLineNo}{1}%
    #1\setbox0=\hbox{#1\the\verbatim@line}%
    \hsize=\wd0 \the\verbatim@line\par}}%
  \@minipagetrue%
  \@tempswatrue%
  \global\edef\sv@name{\@macro@name{#2}}%
  \global\edef\cverbboxColor{#4}%
  \global\edef\cverbboxFColor{#5}%
  \@ifundefined{\sv@name content}{%
    \expandafter\newsavebox\expandafter{\csname\sv@name content\endcsname}%
  }%
  \expandafter\global\expandafter\edef\csname\sv@name\endcsname{\usebox{%
  \csname\sv@name content\endcsname}}%
  \setbox0=\vbox\bgroup\color{#3} \verbatim
}
{%
  \endverbatim%
  \unskip\setbox0=\lastbox %
  \egroup%
  \setbox1=\hbox{%
    \colorbox{\cverbboxColor}{\box0}}%
  \global\sbox{\csname\sv@name content\endcsname}%
    {\fboxsep=\fboxrule\colorbox{\cverbboxFColor}{\box1}}%
}
\makeatother
\fboxrule=1pt\fboxsep=3pt\relax
\begin{document}
\lipsum[1]
\begin{figure*}[ht]
\begin{cverbbox}[\textcolor{black}{\scriptsize\theVerbboxLineNo~~}]{\mycvbox}{red!80}{blue!10}{cyan}
\verbatim <Text>
Here
%$#@&^* \macros
xa
\end{cverbbox}
\centerline{\mycvbox}
\caption*{My code chunk}
\end{figure*}
\lipsum[2]
\end{document}

enter image description here

3

This approach is not a simple environment but a standard minted environment nested in you own float environment. Not exactly the asked but:

Each line in a src code block is numbered (optionally)

The font used for a src code block is monospaced (and syntax highlited)

A border is drawn around a code code block, much like \fbox does (with top caption inside using \stdcaption, for a botom caption outside the box use \caption*)

Each code block "floats" to a position (concretely to top of a new page) so that it will fit all on one page.

mwe

\documentclass[a6paper]{article}
\usepackage[margin=1cm, paperheight=5in,paperwidth=5in]{geometry}
\pagestyle{empty}
\usepackage{lipsum}
\let\stdcaption\caption
\setcounter{totalnumber}{1}
\usepackage{minted}
\usepackage{float}
\floatstyle{boxed}
\newfloat{fancycode}{t}{loa}
\floatname{fancycode}{LaTeX code}

\begin{document}

\begin{fancycode} \stdcaption[Basic Structure]{The basic structure of a \LaTeX\ document.} \begin{minted}[linenos,bgcolor=orange!05]{latex} \documentclass{article} \begin{document} \end{document} \end{minted} \end{fancycode}

\begin{fancycode} \stdcaption[Preamble]{The \LaTeX\ preamble to do a \texttt{fancycode}.} \begin{minted}[linenos,bgcolor=cyan!10]{latex} \usepackage{minted} \usepackage{float} \floatstyle{boxed} \newfloat{fancycode}{t}{loa} \floatname{fancycode}{LaTeX code} \end{minted} \end{fancycode}

\lipsum[1-2]

\listof{fancycode}{Code examples}

\end{document}

Fran
  • 80,769