章节仅在目录中,不在文档正文中

章节仅在目录中,不在文档正文中

是否有用于章节的更新命令或类似命令,以便我可以抑制章节名称出现在文档正文中,但会显示在目录中?

这是我得到的最接近的答案,但是我需要在每一章中使用它,如果能在一个地方对整个文档进行操作就好了。

\addcontentsline{toc}{chapter}{chapter name as its displayed in toc}

背景是我正在写一本诗集,并希望诗歌的名字仅显示在目录中。

答案1

尝试此代码

 \documentclass[12pt,a4paper]{book}

\begin{document}
    
    \tableofcontents
    
    \chapter*{}
    \addcontentsline{toc}{chapter}{chapter name as its displayed in toc}
    Some text
    
\end{document}

更新后续问题之后

此代码允许您使用该\chapter命令。它不会在文档中生成章节标题,但会将其显示在目录中。您也可以隐藏章节编号。

 \documentclass[12pt,a4paper]{book}
 
% ******************************** added <<<<
 \usepackage{xpatch} 
 \makeatletter
 \xpatchcmd{\@makechapterhead} {\@chapapp\space\thechapter}{}{}{}
 \xpatchcmd{\@makechapterhead}{#1}{}{}{}
 %\xpatchcmd{\@chapter}{\protect\numberline{\thechapter}}{}{}{}  % OPTIONAL supress chapter number <<<<
 \makeatother
%************************************* 

\begin{document}
    
    \tableofcontents
        
    \chapter{chapter name as its displayed in toc}
    
    Some text
    
\end{document}

答案2

感谢 Simon Dispa 的回复,这个问题现在解决了!我将在这里添加我的最终设置,以防将来有人遇到此问题:

因此在普通的 LaTeX 中它将是

\documentclass[11pt]{book}
\usepackage{setspace}
\usepackage{titletoc}
\usepackage{xpatch}
\usepackage{hyperref}
\hypersetup{colorlinks, linkcolor=black}
\makeatletter
\xpatchcmd{\@makechapterhead}
{\@chapapp\space\thechapter}{}{}{}
\xpatchcmd{\@makechapterhead}{#1}{}{}{}
\makeatother
\pagestyle{plain}
\titlecontents{chapter}[0em]{}{}{\itshape\bfseries}{\hfill\contentspage}[\medskip]
\author{Charles Bukowski}
\date{\today}
\title{Poetry Collection\\\medskip
\large Template}

\begin{document}

\maketitle
\tableofcontents

\setstretch{1.2}
\chapter{now}
\begin{verse}
I sit here on the 2nd floor\\\empty
hunched over in yellow\\\empty
pajamas\\\empty
still pretending to be\\\empty
a writer.\\\empty
\end{verse}

\chapter{next poem}
\begin{verse}
WRITE HERE\ldots{}\\\empty
\end{verse}

\end{document}

但是,我正在使用 Org 模式,因此我的设置方法如下:

首先,安装文件(为了避免使主文档混乱)

# POETRY COLLECTION SETUPFILE
# Saved as poetry-collection-setupfile.org in same directory as main org document

#+LATEX_CLASS:  book
# Table of content (toc:nil to disable)
#+OPTIONS: toc:t
# Needed for setstretch
#+LATEX_HEADER: \usepackage{setspace}
# Needed for titlecontents
#+LATEX_HEADER: \usepackage{titletoc}
# Needed for xpatchcmd
#+LATEX_HEADER: \usepackage{xpatch}
# Links without color
#+LATEX_HEADER: \usepackage{hyperref}
#+LATEX_HEADER: \hypersetup{colorlinks, linkcolor=black}

# Remove chapter showing in body of document (depends on xpatch package)
#+LATEX_HEADER: \makeatletter
#+LATEX_HEADER: \xpatchcmd{\@makechapterhead}
#+LATEX_HEADER: {\@chapapp\space\thechapter}{}{}{}
#+LATEX_HEADER:  \xpatchcmd{\@makechapterhead}{#1}{}{}{}
#+LATEX_HEADER:  \makeatother

# Remove header and give a more clean minimal look in general
#+LATEX_HEADER: \pagestyle{plain}

# Remove "Part" (first level heading is ignored)
#+LATEX_HEADER: \renewcommand{\part}[1]{}

# Comfortable line spacing (depends on setspace package)
#+LATEX_HEADER: \setstretch{1.2}

# Remove numbering for "Chapters" in table of content (depends on titletoc)
#+LATEX_HEADER: \titlecontents{chapter}[0em]{}{}{\itshape\bfseries}{\hfill\contentspage}[\medskip]

然后,主文件:

#+title: Poetry Collection
#+subtitle: Template
#+author: Charles Bukowski
#+date: \today
#+setupfile: ./poetry-collection-setupfile.org

# First level headings are ignored (i.e. no Parts in exported pdf)
* Some reference for you only while writing

# Second headline exported as chapter in latex pdf
** now
#+begin_verse
I sit here on the 2nd floor
hunched over in yellow
pajamas
still pretending to be
a writer.
#+end_verse

** next poem
#+begin_verse
WRITE HERE...
#+end_verse

此外,如果希望目录位于末尾而不是标题之后,请在设置文件中将 toc:t 设置为 toc:nil,然后在主文档的最后添加

#+begin_export latex
\tableofcontents
#+end_export

相关内容