使用 startsection 格式化时是否保留书签?

使用 startsection 格式化时是否保留书签?

我正在查看以下重新定义的section命令。生成的 PDF 文件中我的所有部分都没有书签。我不想使用新的命令名称。我该如何保留书签?

\renewcommand{\section}[1]{
  \@startsection{section}{0}{0pt}{0pt}{0pt}{\normalfont}*{}
  {
    \large \colorbox{my-gray}{
      \begin{minipage}
        {\textwidth-0.175in}
        {\textbf{#1 }}
      \end{minipage}
    }
  }
}

如果我能做这样的事那就太完美了:

\renewcommand{\section}[1]{
  \@startsection{section}{0}{0pt}{0pt}{0pt}{\normalfont}*{}
  {
    \large \colorbox{my-gray}{
      \begin{minipage}
        {\textwidth-0.175in}
        {<text and format of the section>}
      \end{minipage}
    }
  }
}

更新 20180203:最小可重现示例

这是minimum.tex文件:

% Intended LaTeX compiler: pdflatex
\documentclass[a4]{article}
\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}
\usepackage{graphicx}
\usepackage{grffile}
\usepackage{longtable}
\usepackage{wrapfig}
\usepackage{rotating}
\usepackage[normalem]{ulem}
\usepackage{amsmath}
\usepackage{textcomp}
\usepackage{amssymb}
\usepackage{capt-of}
\usepackage{hyperref}
\usepackage{mystyle}
\hypersetup{colorlinks=true, urlcolor={url-gray}}
\usepackage{enumitem}
\setlist{leftmargin=0.25in,nosep}
\author{Author Name}
\date{\today}
\title{}
\hypersetup{
 pdfauthor={Author Name},
 pdftitle={},
 pdfkeywords={},
 pdfsubject={},
 pdflang={English}}
\begin{document}


\section{ First Section}
\label{sec:org6d66304}
\begin{itemize}
\item Item 1
\item Item 2
\end{itemize}
\section{ Second Section}
\label{sec:org364f276}

\subsection{Sub section 1}
\label{sec:orgee6ac02}
\begin{itemize}
\item Subsec Item 1
\item Subsec Item 2
\end{itemize}
\subsection{Sub section 2}
\label{sec:org3a19fb4}
\begin{itemize}
\item Subsec 2, item 1
\item Subsec 2, item 2
\end{itemize}

\end{document}

mystyle.sty与位于同一目录中minimum.tex

\RequirePackage{latexsym,amsmath,amssymb,charter,color,calc,hyperref,titlesec}
\RequirePackage[empty]{fullpage}

\raggedbottom
\raggedright
\setlength{\tabcolsep}{0in}
\addtolength{\oddsidemargin}{-0.25in}
\addtolength{\evensidemargin}{-0.25in}
\addtolength{\textwidth}{0.5in}
\addtolength{\topmargin}{-0.25in}
\addtolength{\textheight}{0.5in}

% <<< other definitions
\definecolor{my-gray}{gray}{0.90}
\definecolor{url-gray}{gray}{0.25}
% >>>

%% Enumerations
\def\labelitemi{--}

%% Section Headings, etc.
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\newcommand{\titlebar}{%
\Large\scshape\raggedright
% \colorbox{my-gray}
% \textwidth{-0.175in}
}%

\titleformat{\section} %command
            [block] %shape
            {\titlebar} %format
            %% {\bfseries} %format
            {} %label
            {0pt} %sep
            {} %before-code
            [\titlerule] %after-code

\renewcommand{\section}[1]{
  \@startsection{section}{1}{0pt}{0pt}{0pt}{\normalfont}*{}
  {
    \large \colorbox{my-gray}{
      \begin{minipage}
        {\textwidth-0.175in}
        {\textbf{#1}}
      \end{minipage}
    }
  }
}

\titleformat{\subsection} %command
            [block] %shape
            {\bfseries} %format
            {} %label
            {0pt} %sep
            {} %before-code
            [] %after-code

\titlespacing{\subsection}{0pt}{5pt}{0pt}[{0pt}]

使用pdflatex制作的PDF:

pdflatex --version                                                                                                                                            git:master*
pdfTeX 3.14159265-2.6-1.40.18 (TeX Live 2017)
kpathsea version 6.2.3
Copyright 2017 Han The Thanh (pdfTeX) et al.
There is NO warranty.  Redistribution of this software is
covered by the terms of both the pdfTeX copyright and
the Lesser GNU General Public License.
For more information about these matters, see the file
named COPYING and the pdfTeX source.
Primary author of pdfTeX: Han The Thanh (pdfTeX) et al.
Compiled with libpng 1.6.29; using libpng 1.6.29
Compiled with zlib 1.2.11; using zlib 1.2.11
Compiled with xpdf version 3.04

电流输出: 在此处输入图片描述 正如屏幕截图中所见,“第一部分”和“第二部分”的书签丢失。

答案1

您总是在使用\section*,这就是它无法通过的原因。

\renewcommand{\section}{%
  \@startsection{section}{1}{0pt}{0pt}{0pt}{\normalfont\biocyberman@formatsection}%
}
\newcommand{\biocyberman@formatsection}[1]{%
    \large \colorbox{my-gray}{%
      \begin{minipage}
        {\textwidth-0.175in}
        {\textbf{#1}}
      \end{minipage}%
    }%
  }%
}

\section命令应该以没有参数的方式定义;指定的最后一个参数\@startsection应该以一个用于格式化标题的参数宏结束。

在此处输入图片描述

titlesec

%% Section Headings, etc.
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

\titleformat{\section} %command
  [block] %shape
  {\large\bfseries} %format
  {} %label
  {0pt} %sep
  {\biocyberman@formatsection} %before-code

\newcommand{\biocyberman@formatsection}[1]{%
  \colorbox{my-gray}{%
    \begin{minipage}{\textwidth-0.175in}
    \bfseries#1
    \end{minipage}%
  }%
}

\titleformat{\subsection} %command
  [block] %shape
  {\bfseries} %format
  {\thesubsection} %label
  {1em} %sep
  {} %before-code

\titlespacing{\subsection}{0pt}{5pt}{0pt}

在此处输入图片描述

相关内容