如何用 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