如何使用自定义环境或命令来格式化代码片段?

如何使用自定义环境或命令来格式化代码片段?

我想创建一个环境或命令来包装这个2 列 REPL 格式。例如,作为命令 ( \format{TEXT}{CODE}) 它看起来像这样:

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

有了这样的快捷方式可以大大清理我的文档!

它看起来是这样的,左边是文本,右边是代码:

在此处输入图片描述 这里的困难在于 LaTex不允许传递逐字参数。所以我陷入了困境。

梅威瑟:

\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} 

答案1

为什么不改用tcolorbox呢?

您可以创建自己的tcolorboxmycodebox,在我的 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[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} 

在此处输入图片描述

相关内容