PanBook icon indicating copy to clipboard operation
PanBook copied to clipboard

取消listings作为默认代码高亮

Open annProg opened this issue 6 years ago • 9 comments

由于listings有一些语言不支持,默认改为使用pandoc代码高亮。

见 https://github.com/annProg/PanBook/tree/master/test/highlight

annProg avatar Jun 10 '19 09:06 annProg

minted支持mathescape,考虑要不要加入minted

annProg avatar Jun 10 '19 10:06 annProg

默认的代码高亮不支持自动换行,可以pass了。

annProg avatar Jun 10 '19 11:06 annProg

以上问题可以解决

\usepackage{fvextra}
\DefineVerbatimEnvironment{Highlighting}{Verbatim}{breaklines,commandchars=\\\{\}}

see https://tex.stackexchange.com/questions/179926/pandoc-markdown-to-pdf-without-cutting-off-code-block-lines-that-are-too-long

annProg avatar Jun 10 '19 11:06 annProg

默认代码高亮加pandoc-crossrefcodelistings环境,存在长代码分页问题

annProg avatar Jun 10 '19 13:06 annProg

使用minted要求有python环境并且安装Pygments,不太适合作为默认选项。

annProg avatar Jun 10 '19 13:06 annProg

fvextra能解决换行,字号问题 https://mirrors.tuna.tsinghua.edu.cn/CTAN/macros/latex/contrib/fvextra/fvextra.pdf

annProg avatar Jun 10 '19 15:06 annProg

pandoc 定义了一些命令去高亮代码,比如 NormalOK,这些命令中的连续字符串即使用了breaklines=true, breakanywhere=true 也不能自动换行,该考虑放弃了。

另外,listings支持的高亮语言有限,可能minted是最好的选择,mintedfvextra是同一个作者,很多选项都是通用的,能自动换行,能改字号,~~也能换页~~。

以下代码,pandoc定义命令外的无空格长字符串可以换行,\NormalTok包裹的无空格字符串无法自动换行(中文可以正常换行)。

\begin{Highlighting}[numbers=left,breaklines=true,breakanywhere=true,breakbefore=C,firstnumber=100,]
\FunctionTok{# Chapter 1}
LongCodeLongCodeLongCodeLongCodeLongCodeLongCodeLongCodeLongCodeLongCodeLongCodeLongCodeLongCodeLongCodeLongCodeLongCodeLongCodeLongCodeLongCodeLongCodeLongCodeLongCodeLongCodeLongCodeLongCodeLongCodeLongCodeLongCode

\NormalTok{LongCodeLongCodeLongCodeLongCodeLongCodeLongCodeLongCodeLongCodeLongCodeLongCodeLongCodeLongCodeLongCodeLongCodeLongCodeLongCodeLongCodeLongCodeLongCodeLongCodeLongCodeLongCodeLongCodeLongCodeLongCodeLongCodeLongCode}

\NormalTok{长代码长代码长代码长代码长代码长代码长代码长代码长代码长代码长代码长代码长代码长代码长代码长代码长代码长代码长代码长代码长代码长代码长代码长代码长代码长代码长代码长代码长代码长代码长代码长代码}

annProg avatar Jun 10 '19 15:06 annProg

跨页问题

  1. listings无float选项时可以正常跨页
  2. 原生高亮及minted会被pandoc-crossref增加codelistings环境,是一个float环境,存在跨页问题

annProg avatar Jun 11 '19 02:06 annProg

https://tex.stackexchange.com/questions/175650/how-to-allow-page-break-inside-a-float-environment

Floats cannot be broken across pages—they will continue to float until they fit a page or the document ends, whichever comes first. It appears (from usage of !h, which is not recommended, by the way) that you're trying to absolutely position this float anyway.

看来长代码只能手动分成多个短代码块了

annProg avatar Jun 11 '19 02:06 annProg