# How to format code snippets with a custom environment or command?

I'd like to make an environment or command to wrap the this 2-column REPL formatting. For example, as a command (\format{TEXT}{CODE}) it would look like this:

\format2cols{
Description of the code
}{
The actual code
}


Having a shortcut like this would clean up my document exponentially!

Here's what it looks like, with the TEXT is on the left and the CODE on the right:

The difficulty here is that LaTex doesn't allow for passing verbatim arguments. So I'm at an impasse.

MWE:

\documentclass{book}
\usepackage{a4wide}
\usepackage{tabularx}
\usepackage[]{sourcecodepro}
\usepackage[T1]{fontenc}
\usepackage{ragged2e}

\newlength\textlen
\newlength\codelen
\newlength\halflen
\setlength\halflen{\dimexpr.5\columnwidth-2\tabcolsep-0.5\arrayrulewidth\relax}
\setlength\textlen{.95\halflen}
\setlength\codelen{1.05\halflen}

\usepackage{color}
\definecolor{mygray}{rgb}{0.9,0.9,0.9}

\usepackage{minted}
\setminted{
baselinestretch=1,
tabsize=4,
fontsize=\fontsize{8}{11}
}

\begin{document}
\fontsize{12}{14}\selectfont

\justify
We restrict our sorting functions to permutations of the numbers $1\cdots n$. A simple way to visualize the amount of sortedness'' in such a list of numbers is a picture obtained with {\fontsize{10}{12}\selectfont\texttt{ListPlot}}.
\vspace{1.5mm}

{
\fontfamily{ptm}\selectfont
\fontsize{9}{11}\selectfont
\def\arraystretch{2}
\noindent\begin{tabular}{@{}p{\textlen}p{\codelen}@{}}
This definition is used to plot permutations of the numbers $1\cdots n$ with suitable settings of graphics options. &
\begin{minipage}[t]{0.5\textwidth}
\begin{minted}{Mathematica}
In[1]:= PermutationPlot[l_List, opts___] :=
ListPlot[ l,
PlotRange -> { {0.5, Length[l]+0.5},
{0.5, Length[l]+0.5} },
PlotStyle -> PointSize[0.75/Length[l]],
opts, Axes->None, FrameTicks->None,
Frame->True, AspectRatio->1 ]
\end{minted}
\end{minipage}
\end{tabular}
}
\vspace{1.5mm}

\justify
Sed efficitur diam eget ante elementum pharetra. Nullam dignissim pulvinar molestie. Sed dapibus tristique feugiat.

\end{document}

– user121799
Dec 12, 2017 at 4:16
• @marmot it's in the link, but I'll paste it in
– M.R.
Dec 12, 2017 at 4:19
• Verbatim is alway tricky. I would recommend an environment instead of a macro. Dec 12, 2017 at 6:57
• On another note: using \fontsize and similar macros is not recommended. If you want 12pt as default font size use it as a class option ([12pt]{book}) and then use macros like \small etc. Dec 12, 2017 at 6:59
• @MartinScharrer Thanks I've updated the question to reflect that
– M.R.
Dec 12, 2017 at 8:42

Why don't use tcolorbox instead?

You could create your own tcolorbox (mycodebox, in my MWE) and use it as any other environment, changing only the comment and the code.

\documentclass{book}
\usepackage{a4wide}
\usepackage{tabularx}
\usepackage[]{sourcecodepro}
\usepackage[T1]{fontenc}
\usepackage{ragged2e}

\newlength\textlen
\newlength\codelen
\newlength\halflen
\setlength\halflen{\dimexpr.5\columnwidth-2\tabcolsep-0.5\arrayrulewidth\relax}
\setlength\textlen{.95\halflen}
\setlength\codelen{1.05\halflen}

\usepackage[most]{tcolorbox}
\tcbuselibrary{minted}
\definecolor{mygray}{rgb}{0.9,0.9,0.9}

\setminted{
baselinestretch=1,
tabsize=4,
fontsize=\fontsize{8}{11}
}
\newtcblisting{mycodebox}[2][]{%
comment side listing,
sidebyside align=top,
halign=justify,
colframe=white,
colback=white,
boxrule=0pt,
left=0pt,
right=0pt,
leftrule=0pt,
rightrule=0pt,
bottomrule=0pt,
boxsep=0pt,
after=\vspace{-16pt},
lefthand width=.4\textwidth,
fontupper=\fontfamily{ptm}\selectfont\fontsize{9}{11}\selectfont,
minted language=Mathematica,
minted options={baselinestretch=1,
tabsize=4,
fontsize=\fontsize{8}{11}},
comment={#2},#1}

\begin{document}

\fontsize{12}{14}\selectfont

\justify
We restrict our sorting functions to permutations of the numbers $1\cdots n$. A simple way to visualize the amount of sortedness'' in such a list of numbers is a picture obtained with {\fontsize{10}{12}\selectfont\texttt{ListPlot}}.
\vspace{1.5mm}

\begin{mycodebox}{This definition is used to plot permutations of the numbers $1\cdots n$ with suitable settings of graphics options.}
In[1]:= PermutationPlot[l_List, opts___] :=
ListPlot[ l,
PlotRange -> { {0.5, Length[l]+0.5},
{0.5, Length[l]+0.5} },
PlotStyle -> PointSize[0.75/Length[l]],
opts, Axes->None, FrameTicks->None,
Frame->True, AspectRatio->1 ]
\end{mycodebox}
\begin{mycodebox}{Here is a random permutation.}
In[2]:= p1 = PermutationPlot[
{9,10,6,8,2,4,12,1,7,5,3,11},
DisplayFunction->Identity ];
\end{mycodebox}
\justify
Sed efficitur diam eget ante elementum pharetra. Nullam dignissim pulvinar molestie. Sed dapibus tristique feugiat.
\end{document}


• I'm using Texpad on OSX, and it's giving me some errors regarding pigments, I have pygmentize on my path, is there something else I need to do to get it to work?
– M.R.
Dec 12, 2017 at 22:16
• Thanks for the links, it seems that \usepackage[outputdir=.texpadtmp]{minted} would work, but your code does a \tcbuselibrary instead?
• I figured it out, I had to add the line \write18{} to the source file. Bizarre fix, but now it works! Thanks!