Page LayoutAdd Stuff to ToC under XeLaTeX UTF8

Information and discussion about page layout specific issues (e.g. header and footer lines, page formats, page numbers).
Post Reply
niner
Posts: 7
Joined: Thu Aug 04, 2011 2:10 am

Add Stuff to ToC under XeLaTeX UTF8

Post by niner »

Hi, I am working on my report. I use XeLaTeX and UTF8 for Chinese words. Below is my code. Because I want to add words for chapter title in the table of content, so I use \titlecontents command to modify the "chapter" level label. To differ from the appendix part, I add a boolean variable "isAppendix" into \titlecontents command. My goal is to manually add appendix name (without page number) into toc. However, I have compile error as below mentioning something is zero.
! Missing number, treated as zero.
<to be read again>
{
l.119 ...ine{chapter}{\numberline{}附件一}{}{}}
%
I sense this error coming from the code

Code: Select all

\addtocontents{toc}{\contentsline{chapter}{\numberline{}附件一}{}{}}%
and I also notice that this is because I set \titlecontents and variable "isAppendix" is "false". I tried to set "isAppendix = true" before \addtocontents, but it didn't work. I also tried to set "isAppendix = true" before \begin{document}, and not working again. I stuck here and I need some help.

Does anyone have the solution? Any comment is appreciated. Thank you.

Original code below.

Code: Select all

%!TEX TS-program = xelatex
%!TEX encoding = UTF-8 Unicode
%
\documentclass[12pt,a4paper,twoside]{report}
\setcounter{secnumdepth}{3} %設定章節層級至 1.1.1.1
\setcounter{tocdepth}{3}
%
\usepackage{amsmath, amsthm, amssymb, txfonts, upgreek, amsbsy, mathrsfs}
\usepackage{fontspec}
\usepackage[a4paper,top=3.0cm,bottom=2.54cm,left=2cm,right=2cm,bindingoffset=1cm]{geometry}    % 邊界設定,bindingoffset表示裝訂邊
\usepackage[unicode]{hyperref}  % 為 pdf 設定頁籤,開啟 pdf 檔案檢視時左側會有 bookmark 可用
\usepackage{type1cm}            % 使 Type1 字型可以無段放大縮小
\usepackage{graphicx}           % 可直接插入圖片
\usepackage{float}
\usepackage{caption}
\usepackage{indentfirst}        % 首段縮排
\usepackage{url}                % 文稿中出現的 \url{網址} 能夠隨文斷行
\usepackage{natbib}       % 增加更多文獻引用細節功能
\bibpunct{(}{)}{;}{a}{,}{,}
\usepackage{fancyhdr}           % 設定頁首頁尾用的套件
\usepackage{titlesec, titletoc}
\usepackage{appendix}
\usepackage{ifthen}

%
%======================================================================
%% use xeCJK
\usepackage[BoldFont,SlantFont,CJKnumber,CJKchecksingle]{xeCJK}
\usepackage{xCJKnumb}
\setCJKmainfont{BiauKai}%{標楷體}
\setromanfont{Times New Roman}
\XeTeXlinebreaklocale "zh"
\XeTeXlinebreakskip = 0pt plus 1pt
%
%
\newboolean{isAppendix}
\setboolean{isAppendix}{false}
%
%
%
\titleformat{\chapter}%     %設定章標題 (\chapter) 格式
            {\centering\Huge\bfseries}{%
             \ifthenelse{\boolean{isAppendix}}%
                 {附件}%
                 {第\,\xCJKnumber{\thechapter} \,章}%
            }{1em}{}
%
\titlespacing{\chapter}{0pt}{0em}{2em}  %設定章標題 (\chapter)上下間距
%
\titlecontents{chapter}[0em]
             {}{\normalfont\normalsize\bfseries\makebox[4.1em][l]%
                 {\ifthenelse{\boolean{isAppendix}}%
                           {附件}%   
                           {第\xCJKnumber{\thecontentslabel}章}}%
                 }{}%
             {\titlerule*[0.7pc]{.}\contentspage}
%          
%========================================================================
%
\renewcommand{\contentsname}{目\,\,\,錄}
\renewcommand{\figurename}{圖}
\renewcommand{\tablename}{表}
\renewcommand{\listfigurename}{圖目錄}
\renewcommand{\listtablename}{表目錄}
\renewcommand{\bibname}{參考文獻}
%
% Redefine the second level of Enumerate
\renewcommand{\theenumii}{(\arabic{enumii})}   % (1) (2) (3) ...
\renewcommand{\labelenumii}{\theenumii}
% third level
\renewcommand{\theenumiii}{\Alph{enumiii}、}  % A, B, C...
\renewcommand{\labelenumiii}{\theenumiii}
%
%
%======================================================
%====== 開始本文 ========================================
\begin{document}
%
\pagenumbering{roman}
\tableofcontents
\listoffigures
\listoftables
\clearpage
%
% 正文開始的環境設定
% ================================================================================
\pagenumbering{arabic}              % 頁碼改以阿拉伯數字表示

\captionsetup{labelsep=quad}  %修改圖表號,拿掉冒號, eg. 圖1.1  

\fontsize{12pt}{18pt}\selectfont    % 行高 1.5
% 
%
%===========================================================================

% for testing
\chapter{第一名}
測試測試 test test

test test 測試測試 test test測試測試 test test測試測試 test test測試測試 test test

\section{Test 測試}
test test 測試測試 test test測試測試 test test測試測試 test test測試測試 test test

\clearpage
\section{測試 Test}
test test 測試測試 test test測試測試 test test測試測試 test test測試測試 test test
123456

ABCDFG $\alpha \beta$ 測試!測試!測試!

%
%%
%======= Appendix ========================
%
\setboolean{isAppendix}{true}

%\addcontentsline{toc}{chapter}{附件一}
\addtocontents{toc}{\contentsline{chapter}{\numberline{}附件一}{}{}}%
%
\end{document}

Last edited by niner on Fri Aug 26, 2011 9:27 am, edited 1 time in total.
Niner@Taiwan

Recommended reading 2024:

LaTeXguide.org • LaTeX-Cookbook.net • TikZ.org

NEW: TikZ book now 40% off at Amazon.com for a short time.

And: Currently, Packt sells ebooks for $4.99 each if you buy 5 of their over 1000 ebooks. If you choose only a single one, $9.99. How about combining 3 LaTeX books with Python, gnuplot, mathplotlib, Matlab, ChatGPT or other AI books? Epub and PDF. Bundle (3 books, add more for higher discount): https://packt.link/MDH5p

niner
Posts: 7
Joined: Thu Aug 04, 2011 2:10 am

Add Stuff to ToC under XeLaTeX UTF8

Post by niner »

It seems like no one can give a hint on my question. I have another thought. I can use

Code: Select all

\addcontentsline{toc}{chapter}{Any Chinese Words}
to add a line in toc. But I don't want to show the page number and the dotted line (as those for Chapter in usual toc). Is there a way to do this? Can I use command \addcontentsline without page number? :?:

I have tried many times, and no use at the end. I need the help. Any comment is appreciated. Thank you.
Niner@Taiwan
niner
Posts: 7
Joined: Thu Aug 04, 2011 2:10 am

Add Stuff to ToC under XeLaTeX UTF8

Post by niner »

I solved my problem. I add the following code for appendix.

Code: Select all

\addtocontents{toc}{\protect\setboolean{isAppendix}{true}}
\addtocontents{toc}{%
	\protect\contentsline{chapter}{\textbf{附件一}}{}{}}
Then, when the LaTeX generated ToC, it will make isAppendix=true before adding the appendix into ToC.

Lesson learned. ;)
Niner@Taiwan
Post Reply