如何用 zettlr 正确导出文章( pdf 和 doc 格式)


安装「Pandoc」和「LaTex」

zettlr 并没有自带导出功能,图标上的导出看起来很靓仔很全面,但是必须安装这两个额外工具才能正常使用。(虽然它只显示需要 LaTex ,但是不安装 Pandoc 也是没法运作的)

安装 Pandoc :菜单栏→ 帮助→ 安装 Pandoc → 自动打开 Pandoc 的 github 版本页面→ 选择最新版(目前是2.14)下载安装即可

注:在解决导出问题的时候死马当活马医安装了旧版本(2.10),不过没什么效果。但是可以作为备用选项,如果接下来的方法无用就可以尝试使用旧版

安装 LaTex :菜单栏→帮助→ 打开用户手册(自行翻译)→ 左侧栏选择「Installing LaTex」→ 我选择 Windows 系统的「MikTeX 」→ 自动打开下载界面,下载安装即可

注:如果只需要在这个软件内进行导出工作,只需要下载这个「MikTeX」也就是最小版就可以了,不需要占空间更大的完全版。以下的流程也建立在使用「MikTeX」的前提下,完全版的调整我不懂

两个软件的安装步骤都没什么特别的,狂按下一步就可以了

好了,到这一步本来就可以了。选择导出,然后等一会 (不会有进度条,就干等) 就会让你选择打开方式,大功告成。但遗憾的是因为对中文支持的欠缺,导出的 pdf 只会是一堆「口口」,字体无法正确输出

切换字体,处理换行错误

参考资源:1.这个视频下的网友海风云vic、小心桃子、CROWDS21提供的信息
2.zettlr模版代码

处理乱码:

  1. 安装 思源宋体-汉语(SourceHanSerifCN),已经安装的就不用重复安装了
  2. 菜单→ 偏好→ PDF选项→ 字体→ 主要次要字体都修改为思源宋体 CN
  3. 管理员模式编辑\MiKTeX(看你装在哪,默认在 C 盘的 program files 里)\tex\xelatex\xetexconfig\geometry.cfg(我只找到 crop.cfg 这个文件,我就当是这个吧)。开始处增加两行:
\XeTeXlinebreaklocale zh  
\XeTeXlinebreakskip = 0pt plus 1pt

到这一步再选择导出 pdf ,应该就可以输出汉字了。只是「换行」会出现错误,表现为一整行内容就打印能打印的,剩下的就撂在虚空里找不着了(即显示不全)。现在就需要处理 zettlr 的模版来解决问题。

处理换行错误:

因为没有找到直接修改 zettlr 默认模版的方法,所以就根据网友提供的资料找了一份默认模版的代码,直接新建一个 txt 文档然后填进去再修改后缀就行,再在此基础上做修改:要修改部分的默认代码为

\documentclass[
$FONT_SIZE$,
$if(lang)$
  $babel-lang$,
$endif$
]{scrartcl}

把最后这个「scrartcl」改成「ctexart」,或者是 ctexrep, ctexbook, ctexbeamer 。保存一下,大功告成。

额外知识点 1 :CTeX是一个宏集,总之是一个帮助中文论文或书籍排版的工具,分成上面说的 4 种类型,分别对应「文章」、「报告」、「书籍」、「某种幻灯片」。需要哪种用哪种,CTeX是许多大佬的努力成果。这是他们的官网,点我

额外知识点 2 :我过去就想过,书籍是用什么排版的?虽然这个工具主要被用作论文的排版,但是也向我打开了一扇小门,算是多了一个知识吧。知识点 1 结尾的官网里面有关于 Tex 的介绍,这里再贴两个(内容其实一样) LaTex 的网站。书版博客版。让人看了就不会害怕排版。我从这里学到了如何让中英文混排不会显示不全的额外方法

最后提供已经修改好的代码,新建文档粘贴进去,然后修改名字为export.tex。打开 zettlr → 菜单→ 文档→ 偏好→ pdf 选项→ 元数据页面「定制 Tex 模版」,把刚才建好的模版文件选中就可以了。

现在,导出的 pdf 就会正常又漂亮了,像一本书一样。这个时候我试了一下 doc 文档导出,总之也是没问题的,但是默认字体有点丑,我修改成了「等距更纱黑体 SC」,漂亮地很

注:目前这种方法还有些小 bug ,比如说中英文混合的时候英文太长了也会显示不全面。但是问题不大(因为大多数情况下用不到这么长的代码)。此外这个方法会让中文自动缩进

号外号外! 另外还找到一种方法可以让中英文正常。就是把刚才的内容改为\documentclass[UTF8]{ctexart},不过目前会让字体便小一些,有时候可用。

(复制不全的可以在参考资源里复制)

% Barebone template for use with Zettlr exporting engine

% We have to use KOMA classes (scrartcl instead of article and scrreprt instead of 
% report as well as scrbook instead of book)
% More info: https://ctan.org/pkg/koma-script?lang=de
\documentclass[
$FONT_SIZE$,
$if(lang)$
  $babel-lang$,
$endif$
]{ctexart}

% The following packages are needed for the combined use of Pandoc and XeTeX engine
\usepackage{fontspec,xltxtra,xunicode}

% These directives are necessary for Pandoc to correctly set the language of the
% document. If the user uses XeLaTeX (the default for Zettlr), it will use
% polyglossia, else it will use babel. We've simply copied that from the default
% template -- thanks to the Pandoc community at this point!
$if(lang)$
\ifxetex
  \usepackage{polyglossia}
  \setmainlanguage[$polyglossia-lang.options$]{$polyglossia-lang.name$}
$for(polyglossia-otherlangs)$
  \setotherlanguage[$polyglossia-otherlangs.options$]{$polyglossia-otherlangs.name$}
$endfor$
\else
  \usepackage[shorthands=off,$for(babel-otherlangs)$$babel-otherlangs$,$endfor$main=$babel-lang$]{babel}
\fi
$endif$

% Captions for tables and images
\usepackage{caption}
\captionsetup{format=plain, font=small, labelfont=bf}

% This package is needed for better rendering of images (including captions)
\usepackage{graphicx}

% These packages are needed by pandoc for certain special glyphs, such as the
% \square symbol for checkboxes.
\usepackage{amssymb,amsmath}

% Calc is needed to compute the image width (to prevent up-scaling of small images)
\usepackage{calc}

% ulem is needed for underlining and strikethrough text commands.
% the option "normalem" is needed to preserve _italics_. Without the option,
% _italics_ will become underlines, which is not desirable.
\usepackage[normalem]{ulem}

% Image scaling code from pandoc default.latex template
\makeatletter
\def\maxwidth{\ifdim\Gin@nat@width>\linewidth\linewidth\else\Gin@nat@width\fi}
\def\maxheight{\ifdim\Gin@nat@height>\textheight\textheight\else\Gin@nat@height\fi}
\makeatother
% Scale images if necessary, so that they will not overflow the page
% margins by default, and it is still possible to overwrite the defaults
% using explicit options in \includegraphics[width, height, ...]{}
\setkeys{Gin}{width=\maxwidth,height=\maxheight,keepaspectratio}

% Now we also have to make sure that the figures Pandoc inserts are positioned
% approximately where they are at. Therefore we have to overwrite the
% \begin{figure} command a little bit by using the float package:
\usepackage{float}
\floatplacement{figure}{H} % ensure default position as H (exactly where the figure is in the text)

% Needed for pandoc's table generation
\usepackage{longtable, booktabs, array}

% Optional packages that Zettlr makes use of
% fontspec is used to set the different fonts to the user's whishes
\setmainfont{$MAIN_FONT$}
\setsansfont{$SANS_FONT$}

% With geometry we can let the user decide on the margin
\usepackage{geometry}
% Additional options for geometry can be found here: http://texdoc.net/texmf-dist/doc/latex/geometry/geometry.pdf
\geometry{verbose,$PAPER_TYPE$,tmargin=$TOP_MARGIN$,bmargin=$BOTTOM_MARGIN$,lmargin=$LEFT_MARGIN$,rmargin=$RIGHT_MARGIN$}

% PAGE NUMBERING OPTIONS: https://de.sharelatex.com/learn/Page_numbering
\pagenumbering{$PAGE_NUMBERING$}

% Line spacing
\usepackage{setspace}

% Can be a multiple of 1 (i.e. 1.5 for 150 percent)
\setstretch{$LINE_SPACING$}

% PDF metadata generated by Zettlr or given by the user, also some other URL
% options (breaklinks breaks URLs so they aren't ugly)
\usepackage[hyphens]{url} % This package breaks links even better
\usepackage{hyperref}
\hypersetup{
unicode=true,
breaklinks=true,
colorlinks=true,
linkcolor=[rgb]{0.00,0.00,0.00},
urlcolor=[rgb]{0.00,0.00,0.00},
pdftitle={$PDF_TITLE$},
pdfsubject={$PDF_SUBJECT$},
pdfauthor={$PDF_AUTHOR$},
pdfkeywords={$PDF_KEYWORDS$},
pdfproducer={Zettlr with Pandoc and XeLaTeX},
pdfborder={0 0 0}
}

% Setup pandoc code highlighting
% pandoc uses an if statement, but until we have code
% to enable or disable highlighting we'll ignore that.

$highlighting-macros$


% Set the date (either \today or a date from the frontmatter)
\date{$PDF_DATE$}

% The following commands must be provided for Pandoc to work correctly.
\providecommand{\tightlist}{%
  \setlength{\itemsep}{0pt}\setlength{\parskip}{0pt}}

% The following commands are used by Pandoc if a CSL bibliography is present
% to create proper indentation, especially for styles with hanging indentation.
$if(csl-refs)$
\newlength{\cslhangindent}
\setlength{\cslhangindent}{1.5em}
\newlength{\csllabelwidth}
\setlength{\csllabelwidth}{3em}
\newenvironment{CSLReferences}[2] % #1 hanging-ident, #2 entry spacing
 {% don't indent paragraphs
  \setlength{\parindent}{0pt}
  % turn on hanging indent if param 1 is 1
  \ifodd #1 \everypar{\setlength{\hangindent}{\cslhangindent}}\ignorespaces\fi
  % set entry spacing
  \ifnum #2 > 0
  \setlength{\parskip}{#2\baselineskip}
  \fi
 }%
 {}
\usepackage{calc}
\newcommand{\CSLBlock}[1]{#1\hfill\break}
\newcommand{\CSLLeftMargin}[1]{\parbox[t]{\csllabelwidth}{#1}}
\newcommand{\CSLRightInline}[1]{\parbox[t]{\linewidth - \csllabelwidth}{#1}\break}
\newcommand{\CSLIndent}[1]{\hspace{\cslhangindent}#1}
$endif$

\title{$PDF_TITLE$}
\author{$PDF_AUTHOR$}

\begin{document}

% Title page?
$TITLEPAGE$

% Generate a table of contents?
$GENERATE_TOC$

% LIST OF TABLES?
$if(lot)$
\listoftables
$endif$

% LIST OF FIGURES?
$if(lof)$
\listoffigures
$endif$

% BEGIN BODY
$body$

\end{document}

简化版

  1. 安装 Pandoc 和 MikTex
  2. 菜单→ 偏好→ PDF选项→ 字体→ 主要次要字体都修改为思源宋体 CN
  3. MiKTeX\tex\xelatex\xetexconfig\geometry.cfg或crop.cfg→ 增加两段:\XeTeXlinebreaklocale zh
    \XeTeXlinebreakskip = 0pt plus 1pt
  4. 自行创建一份zettlr模版,把\documentclass下最后的中括号内容从「scrartcl」改成「ctexart」,或者是 ctexrep, ctexbook, ctexbeamer
  5. 或者改为\documentclass[UTF8]{ctexart}也可以,只是字会小一些,但是英文代码过长不会乱
  6. 提示:用 Typora 或者 obsidian导出 pdf 其实就不会有这么多问题……,可以在这里编辑然后去那边导出的
  7. 用 Typora 导出 github 模版的 pdf ,真的好好看,美哭了
  8. 但是用这个软件写文本,我觉得没毛病


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM