通常在文档中添加代码时LaTeX
,我会使用listings
包。尝试修改和个性化列表中的样式,@1010011010 建议使用该minted
包。
坦率地说,我印象深刻!我已经成功安装了它,并根据我的需求和品味让它工作。我添加了行号,并使用该mdframed
包添加了沿页面分隔的彩色背景。目前,当行太大而无法容纳时,我试图将其分隔开\textwidth
。我使用了@MarcoDaniel 的答案围绕铸造环境的盒子打破界限,但它没有按预期工作。
我的代码是
\documentclass{book}
\usepackage[]{minted}
\usepackage{tcolorbox}
\usepackage{lineno}
\usepackage{mdframed}
\def\gobble#1{}
\renewcommand\DeleteFile[1]{}
\usepackage{xparse}
\ExplSyntaxOn
\box_new:N \l_fvrb_box
\tl_new:N \l_fvrb_tl
\RenewDocumentCommand \FancyVerbFormatLine { m }
{
\hbox_set:Nn \l_fvrb_box { #1 }
\dim_compare:nNnTF { \box_wd:N \l_fvrb_box }>{ \linewidth }
{%box to big
\tl_set:Nn \l_fvrb_tl { #1 }
\fvrb_use_tl:N \l_fvrb_tl
}
{%box fits
\box_use:N \l_fvrb_box
}
}
\cs_new:Npn \fvrb_use_tl:N #1
{
\group_begin:
\null\hfill\vbox_set:Nn \l_fvrb_box
{\hsize=\linewidth
\renewcommand\thelinenumber
{
\ifnum\value{linenumber}=1\relax\else
$\rightarrow$
\fi
}
\begin{internallinenumbers}
\advance\hsize by -2em
\hspace*{-2em}\tl_use:N #1
\end{internallinenumbers}
}
\box_use:N \l_fvrb_box
\group_end:
}
\ExplSyntaxOff
\usepackage{etoolbox}
\usepackage{xcolor}
\definecolor{lbcolor}{rgb}{0.9,0.9,0.9}
\usepackage[margin=2.8cm]{geometry}
\begin{document}
\section{Section}
\begin{minted}[linenos=true,bgcolor=lbcolor,numberblanklines=true,showspaces=false]{java}
int slotADC = 22;// Change CAMAC controller's backpanel jumper, accordingly
int count = 0;
int[] data = new int[1];
int[] offset = {0, 0};
int[] in = {1, 2};
int[] ones = {1,1};
report.setProperty("INSERT", "["+now.format(new Date())+"] Acquisition Started\n");
report.setProperty("INSERT", "["+now.format(new Date())+"] I am set to read "+evSize+" detectors.\n");
Initialize();
\end{minted}
\begin{mdframed}[linecolor=black, topline=true, bottomline=true,
leftline=false, rightline=false, backgroundcolor=lbcolor,userdefinedwidth=\textwidth]
\begin{minted}[fontsize=\normalsize, linenos, frame=lines]{java}
import java.awt.event.*;//for ActionListener
public class Runtime implements KmaxRuntime {
KmaxToolsheet tlsh;//Store a reference to the toolsheet environment
KmaxWidget wSlot;
KmaxWidget wAdd;
KmaxWidget wData;
KmaxWidget dataField;
KmaxWidget countField;
KmaxWidget setSRQ;
KmaxDevice dev;
KmaxHist hist1,hist2,hist3,hist4,hist5,hist6, histoLowX, histoLowY;
KmaxHist dee1;
KmaxWidget report;
KmaxWidget liveTime;
KmaxWidget realTime;
KmaxWidget deadTime;
KmaxWidget progress;
KmaxWidget xcombo, ycombo, offsetButton;
KmaxWidget combo, checkBoxCalib;
/** ADC Levels */
KmaxWidget lld;
KmaxWidget setLLD;
KmaxWidget getLLD;
KmaxWidget lldText;
KmaxWidget uld;
KmaxWidget setULD;
KmaxWidget getULD;
KmaxWidget uldText;
/** Calibration - Check Box(use calibration) */
boolean eventFileOn = true;
int slotADC = 22;// Change CAMAC controller's backpanel jumper, accordingly
int count = 0;
int[] data = new int[1];
int[] offset = {0, 0};
int[] in = {1, 2};
int[] ones = {1,1};
int points = 10;
int[] region = new int[points];
int timeTicks;
int totalTime;
int LiveTime = 0;
int totcount;
int evType = 1; //The Type ID for these kinds of events
SimpleDateFormat today = new SimpleDateFormat("dd/MM/yyyy");
SimpleDateFormat now = new SimpleDateFormat("HH:mm:ss:SSS");
Map<String,KmaxHist> histograms = new HashMap<String,KmaxHist>();
static final int evSize = 6;//The num of parameters per event of this type
static final int BUF_SIZE = evSize*1000;// Buffer size
static final int LP_MEM_TOP = 0xFFFF00;//Memory size 16MB
static final int READ_START = LP_MEM_TOP - BUF_SIZE;//We start the read/write pointer 1 buffer before the end
\end{minted}
%\inputminted[fontsize=\normalsize, linenos, frame=lines]{java}{DAQ.java}%[linenos=true,bgcolor=lbcolor]{java}{DAQ.java}
\end{mdframed}
\end{document}
当使用其内置的彩色背景选项时,换行似乎工作正常bgcolor
。
使用 时则不会出现这种情况mdframed
。例如,文本会跳出框架,并且在检测到换行符时会出现相当烦人的标记。例如,请看下图中的第 39 行和第 60 行
有什么办法可以修复这个问题吗?
答案1
这开发版本通过新选项支持minted
自动换行breaklines
。这里有一种方法可以获得比开发版本目前提供的更好的换行效果。我将使用新选项将其添加到开发版本中,但这可能需要一些时间,因为我预计需要进行一些重构和额外的改进。
在你的序言中添加以下代码,后您已设置所需的突出显示样式。如果您不使用默认样式,请将default
以下代码中的单词替换为您使用的样式的名称。此代码可防止代币。\allowbreak{}
允许相邻标记之间换行,中间没有空格。如果在不需要的位置产生换行,则只需将其删除即可
\makeatletter \let\oldPYGdefault\PYGdefault \def\PYGdefault#1#2{\hbox{\oldPYGdefault{#1}{#2}}\allowbreak{}} \makeatother
这就是 @Thanos 答案中第一个例子所起的作用。第 9 行的倒数第二个字符串在结束引号之前不再断开。
请记住,当标记很长时,防止标记中断可能会产生更糟糕的结果。例如,如果您有一个非常长的字符串,那么它将进入边距。
编辑 2015/01/16:现在的开发版本minted
包含一个breakbytoken
基于此答案的选项。
答案2
一个相当初步的答案
为了让它工作,我下载了最新版本的minted.sty
。
有一些方法可以使其发挥作用,但我觉得还需要一些改进,所以请大家随意!
使用环境的短代码/inputminted
看起来运行良好!
\documentclass{book}
\usepackage[]{minted}
\usepackage{xcolor}
\definecolor{lbcolor}{rgb}{0.9,0.9,0.9}
\usepackage[margin=2.8cm]{geometry}
\begin{document}
\section{Minted Environment}%For a weird reason the code is put at the bottom side of the page
\begin{minted}[linenos=true,bgcolor=lbcolor,numberblanklines=true,showspaces=false,breaklines=true]{java}
int slotADC = 22;// Change CAMAC controller's backpanel jumper, accordingly
int count = 0;
int[] data = new int[1];
int[] offset = {0, 0};
int[] in = {1, 2};
int[] ones = {1,1};
report.setProperty("INSERT", "["+now.format(new Date())+"] Acquisition Started\n");
report.setProperty("INSERT", "["+now.format(new Date())+"] I am set to read "+evSize+" detectors.\n");
Initialize();
\end{minted}
\end{document}
需要跨越一页的大型代码效果不太好。一开始有一个空白页,代码不会在新页面中中断。
\documentclass{book}
\usepackage[]{minted}
\usepackage{xcolor}
\definecolor{lbcolor}{rgb}{0.9,0.9,0.9}
\usepackage[margin=2.8cm]{geometry}
\begin{document}
\section{Minted Environment Large}%This code is too large to fit in a page, yet it does not break
\begin{minted}[linenos=true,bgcolor=lbcolor,numberblanklines=true,showspaces=false,breaklines=true]{java}
int slotADC = 22;// Change CAMAC controller's backpanel jumper, accordingly
int count = 0;
int[] data = new int[1];
int[] offset = {0, 0};
int[] in = {1, 2};
int[] ones = {1,1};
report.setProperty("INSERT", "["+now.format(new Date())+"] Acquisition Started\n");
report.setProperty("INSERT", "["+now.format(new Date())+"] I am set to read "+evSize+" detectors.\n");
Initialize();
int slotADC = 22;// Change CAMAC controller's backpanel jumper, accordingly
int count = 0;
int[] data = new int[1];
int[] offset = {0, 0};
int[] in = {1, 2};
int[] ones = {1,1};
report.setProperty("INSERT", "["+now.format(new Date())+"] Acquisition Started\n");
report.setProperty("INSERT", "["+now.format(new Date())+"] I am set to read "+evSize+" detectors.\n");
Initialize();
int slotADC = 22;// Change CAMAC controller's backpanel jumper, accordingly
int count = 0;
int[] data = new int[1];
int[] offset = {0, 0};
int[] in = {1, 2};
int[] ones = {1,1};
report.setProperty("INSERT", "["+now.format(new Date())+"] Acquisition Started\n");
report.setProperty("INSERT", "["+now.format(new Date())+"] I am set to read "+evSize+" detectors.\n");
Initialize();
int slotADC = 22;// Change CAMAC controller's backpanel jumper, accordingly
int count = 0;
int[] data = new int[1];
int[] offset = {0, 0};
int[] in = {1, 2};
int[] ones = {1,1};
report.setProperty("INSERT", "["+now.format(new Date())+"] Acquisition Started\n");
report.setProperty("INSERT", "["+now.format(new Date())+"] I am set to read "+evSize+" detectors.\n");
Initialize();
int slotADC = 22;// Change CAMAC controller's backpanel jumper, accordingly
int count = 0;
int[] data = new int[1];
int[] offset = {0, 0};
int[] in = {1, 2};
int[] ones = {1,1};
report.setProperty("INSERT", "["+now.format(new Date())+"] Acquisition Started\n");
report.setProperty("INSERT", "["+now.format(new Date())+"] I am set to read "+evSize+" detectors.\n");
Initialize();
int slotADC = 22;// Change CAMAC controller's backpanel jumper, accordingly
int count = 0;
int[] data = new int[1];
int[] offset = {0, 0};
int[] in = {1, 2};
int[] ones = {1,1};
report.setProperty("INSERT", "["+now.format(new Date())+"] Acquisition Started\n");
report.setProperty("INSERT", "["+now.format(new Date())+"] I am set to read "+evSize+" detectors.\n");
Initialize();
\end{minted}
\end{document}
使用更多页面进行大规模代码破解mdframed
似乎运行良好。唯一的问题是换行符出现在一个space
字符中,有时会很不方便。
\documentclass{book}
\usepackage[]{minted}
\usepackage{mdframed}
\usepackage{xcolor}
\definecolor{lbcolor}{rgb}{0.9,0.9,0.9}
\usepackage[margin=2.8cm]{geometry}
\begin{document}
\begin{mdframed}[linecolor=black, topline=true, bottomline=true,
leftline=false, rightline=false, backgroundcolor=lbcolor,userdefinedwidth=\textwidth]
\begin{minted}[fontsize=\normalsize, linenos, frame=lines]{java}
import java.awt.event.*;//for ActionListener
public class Runtime implements KmaxRuntime {
KmaxToolsheet tlsh;//Store a reference to the toolsheet environment
KmaxWidget wSlot;
KmaxWidget wAdd;
KmaxWidget wData;
KmaxWidget dataField;
KmaxWidget countField;
KmaxWidget setSRQ;
KmaxDevice dev;
KmaxHist hist1,hist2,hist3,hist4,hist5,hist6, histoLowX, histoLowY;
KmaxHist dee1;
KmaxWidget report;
KmaxWidget liveTime;
KmaxWidget realTime;
KmaxWidget deadTime;
KmaxWidget progress;
KmaxWidget xcombo, ycombo, offsetButton;
KmaxWidget combo, checkBoxCalib;
/** ADC Levels */
KmaxWidget lld;
KmaxWidget setLLD;
KmaxWidget getLLD;
KmaxWidget lldText;
KmaxWidget uld;
KmaxWidget setULD;
KmaxWidget getULD;
KmaxWidget uldText;
/** Calibration - Check Box(use calibration) */
boolean eventFileOn = true;
int slotADC = 22;// Change CAMAC controller's backpanel jumper, accordingly
int count = 0;
int[] data = new int[1];
int[] offset = {0, 0};
int[] in = {1, 2};
int[] ones = {1,1};
int points = 10;
int[] region = new int[points];
int timeTicks;
int totalTime;
int LiveTime = 0;
int totcount;
int evType = 1; //The Type ID for these kinds of events
SimpleDateFormat today = new SimpleDateFormat("dd/MM/yyyy");
SimpleDateFormat now = new SimpleDateFormat("HH:mm:ss:SSS");
Map<String,KmaxHist> histograms = new HashMap<String,KmaxHist>();
static final int evSize = 6;//The num of parameters per event of this type
static final int BUF_SIZE = evSize*1000;// Buffer size
static final int LP_MEM_TOP = 0xFFFF00;//Memory size 16MB
static final int READ_START = LP_MEM_TOP - BUF_SIZE;//We start the read/write pointer 1 buffer before the end
\end{minted}
\end{mdframed}
\end{document}
答案3
我只是在代码中长字符串的某个位置添加了一个空格,但 minted 认为这个空格不能被拆分成两行。结合 breaklines 选项,我修改了断点,以实现视觉优化。我想说这个方法虽然快速又粗糙,但确实有效!
例子:
my_cool_variable.my_super_cool_function(:what_a_long_name_for_a_parameter).yes_i_needed_to_call_something_more <- broken wrong with breaklines option
my_cool_variable.my_super_cool_function (:what_a_long_name_for_a_parameter) .yes_i_needed_to_call_something_more <- whitespace in the code creates a breakpoint