% \iffalse
%<cm03|cm302>\NeedsTeXFormat{LaTeX2e}[2023-11-01]
%<cm03>\ProvidesClass{nstc-cm03}
%<cm302>\ProvidesClass{nstc-cm302}
%<cm03|cm302>   [2024-11-04 v1.0 NSTC CM03/302 LaTeX 文件格式]
%
%<*driver>
\ProvidesFile{\jobname.dtx}
  [2024/11/04 v1.0 NSTC CM03/302 LaTeX 文件格式]
\documentclass{ltxdoc}
\EnableCrossrefs
\CodelineIndex
\RecordChanges
\usepackage{url}
\usepackage{graphicx}
\usepackage{setspace}
\usepackage{pdfpages}
\onehalfspacing
\usepackage{xltxtra}
\usepackage[CJKspace]{xeCJK}
\usepackage{titlesec,zhnumber}
\renewcommand\labelenumi{(\zhnum{enumi})}
\renewcommand\theenumi\labelenumi
\setmainfont{bkai00mp.ttf}[AutoFakeBold=2.5]
\setCJKmainfont{bkai00mp.ttf}[AutoFakeBold=2.5]
\zhnumsetup{style=Traditional}
\begin{document}
  \DocInput{\jobname.dtx}
%%  \PrintChanges
\end{document}
%</driver>
% \fi
%
% \changes{v1.0}{2024/11/05}{初始版本}
% \GetFileInfo{\jobname.dtx}
% \title{\texorpdfstring{\textsf{nstc-proposal} 套件\thanks{GitHub 專案網站:\url{https://github.com/L-TChen/latex-cm03}}}}
% \author{陳亮廷}
% \date{\zhdate{2024/11/05} \fileversion}
% \maketitle
% 此套件針對國科會中文專題計劃表 CM03/CM302 的需求,提供中英混排、符合規定的文件格式 |nstc-cm03| 以及 |nstc-cm302|。\footnote{唯獨該格式要求設定 Word 的「單行間距」有歧義:根據 Wikipedia 的說明,Word 97–2010 的單行間距實則為 1.15 倍的行距,之後的版本為 1.08 倍,所以不清楚到底行距實質規定多少。此文件格式行距以 |setspace| 套件設定,肉眼比對約略相等國科會提供的範例。}
%
% \titleformat{\section}{\bfseries\Large}{\zhnum{section}、}{0em}{}
% \section{使用方式}
% 本套件提供設定 CM03/302 版面的文件格式以及中文標題名。
%
%
% \subsection{文件格式} 
% 提供以下兩種格式繼承修改自 \LaTeX 內建之 |article| 文件格式,更改 |\documentclass| 即可使用。
%
% \DescribeMacro{nstc-cm03}
% 適用於表 CM03 研究計劃的文件格式。
%
% \DescribeMacro{nstc-cm302}
% 適用於表 CM302 著作目錄的文件格式。
%
% \subsection{文件格式選項}
%
% 兩文件格式皆提供以下選項:
% 
% \DescribeMacro{libertine}
% 更換為 Linux Libertine 字型,較適合搭配楷體漢字。
% 
% \DescribeMacro{kaiti}
% 更換為 macOS 上內建的常州華文「楷體-繁」字型,該字型具備粗體。
%
% \DescribeMacro{draft oneside twoside fleqn leqno}
% 繼承自 |article| 的選項,作用請參考 \LaTeX 中的說明。若使用 |article| 其他不相容於 CM03/302 的選項,仍可編譯但會產生警告訊息。
%
% 
% \subsection{指令} \label{sec:commands}
% 本套件可使用 |article| 格式的指令。此套件另提供以下指令生成表 CM03 中的章節及敘述說明。
%
% \DescribeMacro{\ProposalBackground}
% 「研究計畫之背景\quad 請詳述本研究計畫\dots\dots」
%
% \DescribeMacro{\ProposalMethod}
% 「研究方法、進行步驟及執行進度\quad 請分年列述:\dots\dots」
%
% \DescribeMacro{\ProposalPlan}
% 「預期完成之工作項目及成果\quad 請分年列述:\dots\dots」
%
% \DescribeMacro{\ProposalIntegration}
% 「整合型研究計畫說明\quad 如為整合型研究計畫\dots\dots」
%
% \subsection{預載套件}
% 
% 除 |amsmath| 和 |amsthm| 外,只載入格式設定必要的套件,其他套件請自行載入。
% \subsection{範例}
% 以下為原始檔範例,此說明最後附有編譯結果。
% \subsubsection{CM03 研究計劃}
% 下列之範本可產生如國科會提供之範例,並具有 PDF 目錄結構:
%    \begin{macrocode}
%<*proposal.tex>
\documentclass[libertine]{nstc-cm03}
\usepackage{microtype}
\begin{document}

\ProposalBackground
\ProposalMethod
\ProposalPlan
\ProposalIntegration

\bibliographystyle{plain}
\bibliography{sample}
\end{document}
%</proposal.tex>
%    \end{macrocode}
%
% \subsubsection{CM302 著作目錄}
% 著作目錄可用指令 |\nocite{*}| 將 |.bib| 檔內所有文獻引用,如:
%    \begin{macrocode}
%<*bibliography.tex>
\documentclass{nstc-cm302}
\usepackage[hidelinks]{hyperref}
\usepackage{doi}
\usepackage{microtype}
\begin{document}

\nocite{*}

\bibliographystyle{abbrv}
\bibliography{sample}
\end{document}
%</bibliography.tex>
%    \end{macrocode}
% 
% \section{後記}
% 
% 理工領域學者學術寫作習慣使用英文,專題研究的申請書則仍常用中文,
% 此文件格式試圖滿足中英文混排並與申請書其他文件一致協調,另有以下要求:
% 
% \begin{enumerate}
%   \item 產生之 PDF 檔有相對應的目錄
%   \item 字型使用需合法
%   \item 需相容 pdf\LaTeX
% \end{enumerate}
% 
% 首先,文章結構中的 |\section| 對應中文標題(如:「研究計畫之背景」),章節編號採中文數字(如:一、二、三 )。而子節(|\subsection|)則假設為英文段落,標題後的文字沒有空行隔開,而是間隔一小空白後直接開始,與中文標題一致。因為用標準的 \LaTeX 命令設定,產生出來的 PDF 檔如用 |hyperref| 套件可產生對應的章節目錄。

% 國科會 CM03 範例中的章節文字,本套件提供指令如 |\ProposalBackground| (見~\ref{sec:commands}~節)印出,但並不預設產生。除固定顯示「三、研究計畫內容(以中文或英文撰寫)」外,其他節的標題文字可由使用者自行更改。
% 
% 中文字型選擇配合計畫文件其他頁面,將採用標楷體(若作業系統無提供,則用 \TeX 提供的「文鼎 PL 楷體」)。然而標楷體並無設計粗體,加粗僅為軟體模擬,部分留白處太少。而 macOS 內建常州華文設計的「楷體-繁」,該字型同為楷體且有設計相對應的粗體字重,較為美觀清楚。因此針對 macOS 使用者,提供選項 |kaiti| 改採用「楷體-繁」。
% 
% 至於英文字型,台灣研究所論文常搭配同是襯線字的 Times New Roman,依循慣例以同字體的 TeXGyre Termes 作為預設字型。然而 Times 字體收尾尖銳細長,較相似中文的明體。相較之下,同是襯線字的 Linux Libertine 較圓潤與楷體較為一直,因此提供選項 |libertine| 可改為 Linux Libertine 字型。
% 
% 至於 pdf\LaTeX 仍有編譯快速以及 |microtype| 套件支援完整等優點,因此盡力相容 pdf\LaTeX 不要求使用 \XeTeX 編譯中文。但中文選擇受限於 \TeX 的 T1 字型,標楷體因版權無法製成 T1 字型散佈,改採用文鼎公眾授權的「PL 楷體」\footnote{授權聲明在此:\url{https://www.arphic.com.tw/2022/01/21/plfont/}}替代。
% \paragraph{致謝} 在此特別感謝柯向上博士\footnote{柯博士的個人網頁:\url{https://josh-hs-ko.github.io}},此套件最初基於他提供的頁面設定所發展。
%
% \appendix
%
% \StopEventually{}
%
% \titleformat{\section}{\bfseries\Large}{附錄\Alph{section}、}{0em}{}
% \section{實作}
%
% 此節提供實作細節的說明,供後人修改參考。
% 
% \subsection{文件格式選項}
% \begin{macro}{nstc-cm03.cls/nstc-cm302.cls}
% 這兩個文件格式由同一個檔案生成,格式版面皆相同,差別僅在於部分文字。
% \begin{macro}{}
% 若使用不相容於表 CM03 的 |article| 文件格式的選項,則編譯時會提出警告。
%    \begin{macrocode}
%<*cm03|cm302>
\def\cmthree@opterrlong{The option "\CurrentOption" from article.cls is
  not compatible with the format of cm03.}
\newcommand{\cmthree@warning}{\ClassWarning{cm03}{\cmthree@opterrlong}}
\newcommand{\cmthree@info}[1]{\ClassInfo{cm03}{#1}}
%    \end{macrocode}
% \end{macro}
% \begin{macro}{}
% 以下是不相容的選項:
%    \begin{macrocode}
\DeclareOption{titlepage}{\cmthree@warning}
\DeclareOption{notitlepage}{\cmthree@warning}
\DeclareOption{10pt}{\cmthree@warning}
\DeclareOption{11pt}{\cmthree@warning}
\DeclareOption{letterpaper}{\cmthree@warning}
\DeclareOption{a5paper}{\cmthree@warning}
\DeclareOption{b5paper}{\cmthree@warning}
\DeclareOption{executivepaper}{\cmthree@warning}
\DeclareOption{legalpaper}{\cmthree@warning}
\DeclareOption{twocolumn}{\cmthree@warning}
\DeclareOption{openright}{\cmthree@warning}
%    \end{macrocode}
% \end{macro}
% \begin{macro}{libertine,kaiti}
% 額外提供兩個選項 |libertine| 跟 |kaiti| 改變字型: 
%    \begin{macrocode}
\DeclareOption{libertine}{\let\@libertine\relax}
\DeclareOption{kaiti}{\let\@kaiti\relax}
%    \end{macrocode}
% \end{macro}
% \begin{macro}{}
% 剩下的選項傳入 |article| 文件格式,並且設定頁面跟字型大小:
%    \begin{macrocode}
\DeclareOption*{\PassOptionsToClass{\CurrentOption}{article}}
\ProcessOptions\relax
\LoadClass[a4paper,12pt,onecolumn,twoside]{article}
\RequirePackage[margin=2cm,bottom=2.4cm]{geometry}
\RequirePackage{setspace}
\onehalfspacing

\RequirePackage{iftex,xifthen}
\RequirePackage{amsmath,amsthm}
%    \end{macrocode}
% \end{macro}
%
% \subsection{中文字型處理}
% \begin{macro}{}
% 根據編譯引擎不同,中文字型設定有所不同。因此先用 |iftex| 套件提供之 |\iftutex| 判斷是否為 \XeTeX 。
%    \begin{macrocode}
\iftutex
%    \end{macrocode}
% \end{macro}
% \begin{macro}{}
% 若是使用 \XeTeX 編譯,則載入 |xeCJK| 套件處理中文排版。若有選項 |kaiti| 則載入系統字型「楷體-繁(|Kaiti TC|)」:
%    \begin{macrocode}
\RequirePackage[PunctStyle=plain,no-math]{xeCJK}
\ifx\@kaiti\relax
  \setCJKmainfont{Kaiti TC}[BoldFont = *-Bold]
  \setCJKsansfont{Heiti TC}
  \cmthree@info{使用楷體作為中文字型}
%    \end{macrocode}
% \end{macro}
% \begin{macro}{}
% 若無選項 |kaiti|,則優先選擇系統上的標楷體。然而,標楷體有各種版本,名稱也根據現行編碼習慣略作調整。在中文 Windows 以及 macOS 13.2 以前應皆稱為「標楷體(|BiauKai|)」,在 macOS 13.3 之後稱為「標楷體-繁(|BiauKaiTC|)」:
%    \begin{macrocode}
\else
\IfFontExistsTF{BiauKai}{
  \setCJKmainfont{BiauKai}[AutoFakeBold=2.5]
  \cmthree@info{使用標楷體 (BiauKai) 作為中文字型}
}{
  \IfFontExistsTF{BiauKaiTC}{
  \setCJKmainfont{BiauKaiTC}[AutoFakeBold=2.5]
  \cmthree@info{使用標楷體繁 (BiauKaiTC) 作為中文字型}
%    \end{macrocode}
% \end{macro}
% \begin{macro}{}
% 若無標楷體(|BiauKai| 或 |BiauKaiTC|)則用 |arphic-ttf| 套件的文鼎「PL 楷體」:
%    \begin{macrocode}
}{
  \setCJKmainfont{bkai00mp.ttf}[AutoFakeBold=2.5]
  \cmthree@info{使用 AR PL UKai TW 作為中文字型}
}}
\fi
%    \end{macrocode}
% \end{macro}
% \begin{macro}{}
% 若使用 \LaTeX 編譯,則載入 |CJKutf8| 套件並使用 |arphic| 套件的文鼎「PL 楷體」,並且設定新的 \LaTeX 環境 |zh| ,可在其中使用中文,同時將文件以 |zh| 環境包覆,
%    \begin{macrocode}
\else
  \RequirePackage{CJKutf8}
  \newenvironment{zh}{\begin{CJK*}{UTF8}{bkai}}{\end{CJK*}}
  \input{c70bkai.fd}
  \DeclareFontShape{C70}{bkai}{b}{n}{<-> CJKb * bkaiu}{\CJKbold}
  \AddToHook{begindocument/end}{\begin{zh}}
  \AddToHook{enddocument}{\end{zh}}
\fi
%    \end{macrocode}
% \end{macro}
% \subsection{英文字型處理}
% \begin{macro}{}
%    \begin{macrocode}
\ifx\@libertine\relax
  \RequirePackage[tt=false]{libertine}
  \iftutex
    \RequirePackage{unicode-math}
    \setmathfont[Scale=MatchUppercase]{LibertinusMath-Regular.otf}
  \else
    \RequirePackage[libertine]{newtxmath}
  \fi
%    \end{macrocode}
% \end{macro}
% \begin{macro}{}
% 使用 |newtxtext| 設定英文字型為 \textsf{TeXGyre Termes}。然而,|newtxtext| 會改變 |fontspec| 設定,以至於使用者無法再改字型,而使用者仍可能載入其他設定,所以用 |\defaultfontfeatures{}| 恢復設定:
%    \begin{macrocode}
\else
  \RequirePackage{newtxtext}
  \iftutex
    \defaultfontfeatures{} 
  \fi
  \RequirePackage{newtxmath}
\fi
%    \end{macrocode}
% \end{macro}
% \begin{macro}{}
% \subsection{頁面文字}
% 設定中英文章節格式:
%    \begin{macrocode}
%<cm03>\RequirePackage{zhnumber,titlesec}
%<cm03>\titleformat{\section}[runin]%
%<cm03>  {\bfseries\normalsize}{(\zhnum{section})}{0em}{}
%<cm03>\titleformat{\subsection}[runin]%
%<cm03>  {\bfseries\normalsize}{\thesubsection}{0.5em}{}
%<cm03>\titleformat{\subsubsection}[runin]%
%<cm03>  {\bfseries\normalsize}{\thesubsubsection}{0.5em}{}
%
%    \end{macrocode}
% \end{macro}
% 設定表 CM03/CM302 首頁標題:
% \begin{macro}{}
%    \begin{macrocode}
%<cm302>\def\cmthree@heading{五、著作目錄(建議呈現與計畫相關之著作目錄,頁數以~2~頁為限):}
%<cm302>\def\cmthree@footer{表\;CM302}
%<cm03>\def\cmthree@heading{三、研究計畫內容(以中文或英文撰寫):}
%<cm03>\def\cmthree@footer{表\;CM03}
\AddToHook{begindocument/end}{\par%
  \everypar={{\setbox0=\lastbox}\everypar{}}%
  \begingroup\large\bfseries\cmthree@heading\endgroup\par}

%    \end{macrocode}
% \end{macro}
% \begin{macro}{}
% 設定頁尾格式:
%    \begin{macrocode}
\RequirePackage{lastpage,fancyhdr}
\pagestyle{fancy}
\renewcommand{\headrulewidth}{0pt}
\fancyhead{}
\iftutex
  \lfoot{\cmthree@footer}
\else
  \lfoot{\begin{zh}\cmthree@footer\end{zh}}
\fi
\cfoot{}
\iftutex
  \rfoot{共~\pageref{LastPage}~頁%
  \hspace{1em}第~\thepage~頁}
\else
  \rfoot{\begin{zh}共~\pageref{LastPage}~頁%
  \hspace{1em}第~\thepage~頁\end{zh}}
\fi
%    \end{macrocode}
% \end{macro}
% \begin{macro}{}
% 設定參考文獻標題,表 CM302 令 |\section| 無作用避免錯誤的留白:
%    \begin{macrocode}
%<cm302>\AddToHook{begindocument/end}{\renewcommand{\section}[2]{}}
%<cm03>\AddToHook{begindocument/end}{\renewcommand{\refname}{參考文獻}}
%    \end{macrocode}
% \end{macro}
% \begin{macro}{\zhsection}
% 中文章節的標題與說明透過 |\zhsection| 設定,接續段落預留 1em 的垂直空間。
%    \begin{macrocode}
%<cm03>\newcommand{\zhsection}[2]{\section{#1}#2\vskip1em}
%    \end{macrocode}
% \end{macro}
% \begin{macro}{\ProposalBackground, \ProposalMethod, \ProposalPlan, \ProposalIntegration}
% 設定表 CM03 各節標題文字與說明:
%    \begin{macrocode}
%<cm03>\newcommand{\ProposalBackground}{\zhsection{研究計畫之背景}{請詳述本研究計畫所要探討或解決的問題、研究原創性、重要性、預期影響性及國內外有關本計畫之研究情況、重要參考文獻之評述等。如為連續性計畫應說明上年度研究進度。}}
%<cm03>\newcommand{\ProposalMethod}{\zhsection{研究方法、進行步驟及執行進度}{請分年列述:1. 本計畫採用之研究方法與原因及其創新性。2. 預計可能遭遇之困難及解決途徑。3. 重要儀器之配合使用情形。4. 如為須赴國外或大陸地區研究,請詳述其必要性以及預期效益等。}}
%<cm03>\newcommand{\ProposalPlan}{\zhsection{預期完成之工作項目及成果}{請分年列述:1. 預期完成之工作項目。2. 對於參與之工作人員,預期可獲之訓練。3. 預期完成之研究成果(如實務應用績效、期刊論文、研討會論文、專書、技術報告、專利或技術移轉等質與量之預期成果)。4. 學術研究、國家發展及其他應用方面預期之貢獻。}}
%<cm03>\newcommand{\ProposalIntegration}{\zhsection{整合型研究計畫說明}{如為整合型研究計畫請就以上各點分別說明與其他子計畫之相關性。}}
%</cm03|cm302>
%    \end{macrocode}
% \end{macro}
% \end{macro}
% \includepdfmerge[nup=1x2, landscape]{./example/proposal.pdf, ./example/bibliography.pdf}
%
% \Finale