如何实现自定义段落(或一般章节)编号并允许引用?

如何实现自定义段落(或一般章节)编号并允许引用?

getcurrentref我使用了来自的命令这个问题/答案实现\paragraph使用时的自定义编号。

我将其包装到宏中:

\newcommand\mybook[1]{\paragraph{B \getcurrentref{subsection}-\getcurrentref{paragraph}: #1}\hfill\par\vspace{0.2\baselineskip}}%

它允许我获得这样的结构:

在此处输入图片描述

这似乎不是最好的解决方案,但它确实有效。

基本上我面临的问题是小节应被忽略,段落编号应仅包含当前小节以及后续段落编号。

不过,我想使用自定义编号来引用我的自定义段落,但这并不起作用,正如您在 B 1-2 中看到的那样。

我怎样才能制作\ref甚至更好地 \cref适应我的自定义编号?

我认为它应该可以直接自定义段落命令,例如这里,但我无法让它工作。

有任何想法吗?

我需要一个针对 KOMA 脚本类的解决方案。我需要保留子部分,因为我在文档的其他部分中需要它们。


平均能量损失

\documentclass{scrreprt}
\usepackage{etoolbox}

% get currentref command
\newcommand\getcurrentref[1]{%
 \ifnumequal{\value{#1}}{0}
  {??}
  {\the\value{#1}}%
} 

% remove label from paragraph, even if numbered
\renewcommand*{\paragraphformat}{}

% custom paragraph heading 
\newcommand\mybook[1]{\paragraph{B \getcurrentref{subsection}-\getcurrentref{paragraph}: #1}\hfill\par\vspace{0.2\baselineskip}}%

\setcounter{secnumdepth}{4} % how many sectioning levels to assign numbers to
\setcounter{tocdepth}{2}    % how many sectioning levels to show in ToC  

\begin{document}

\chapter{Books (chapter)}
\section{Fiction (section)}
\subsection{Dystopy (subsection)}

\subsubsection*{George Orwell (subsubsection 1.1.1.1)}

\mybook{Animal Farm (paragraph)}\label{AnimalFarm}

Animal Farm is an allegorical and dystopian novella by George Orwell, first [...]

\mybook{1984 (paragraph)}

Nineteen Eighty-Four, often published as 1984, is a dystopian novel [...]
I'd like to reference Animal Farm (\ref{AnimalFarm}) as B 1-1 [...]

\subsubsection*{Aldous Huxley (subsubsection  1.1.1.2)}

\mybook{Brave New World (paragraph)}

Brave New World is a novel written in 1931 by [...]

\subsection{Historic Novel (subsection)}

\subsubsection*{Ken Follet}

\mybook{The Pillars of the Earth (paragraph)}

\section{Non-Fiction}
\subsection{Cooking}
\subsubsection*{Baking}
\mybook{Baking without flour}
\subsubsection*{Cooking}
\mybook{Cooking without water}

\end{document}

答案1

为什么不重新定义\theparagraph,这将以最简单的方式设置引用:

\documentclass{scrreprt}
\usepackage{etoolbox}

% get currentref command
\newcommand\getcurrentref[1]{%
 \ifnumequal{\value{#1}}{0}
  {??}
  {\the\value{#1}}%
} 

% remove label from paragraph, even if numbered
\renewcommand*{\paragraphformat}{}

% custom paragraph heading 
\newcommand\mybook[1]{\paragraph{B \getcurrentref{subsection}-\getcurrentref{paragraph}: #1}\hfill\par\vspace{0.2\baselineskip}}%

\renewcommand{\theparagraph}{B \arabic{subsection}-\arabic{paragraph}}


\usepackage{cleveref}

\crefname{paragraph}{paragraph}{paragraphs}
\Crefname{paragraph}{Paragraph}{Paragraphs}


\setcounter{secnumdepth}{4} % how many sectioning levels to assign numbers to
\setcounter{tocdepth}{2}    % how many sectioning levels to show in ToC  




\begin{document}


\chapter{Books}
\section{Fiction}
\subsection{Dystopy}

\subsubsection*{George Orwell}

\mybook{Animal Farm}\label{AnimalFarm}

Animal Farm is an allegorical and dystopian novella by George Orwell, first [...]

\mybook{1984}

Nineteen Eighty-Four, often published as 1984, is a dystopian novel [...]

\subsubsection*{Aldous Huxley}

\mybook{Brave New World}

Brave New World is a novel written in 1931 by [...]

\section{Interpretation}

I'd like to Reference Animal Farm (\ref{AnimalFarm}) as B 1-1 [...]

In \cref{AnimalFarm} we saw that Orwell...

\end{document}

在此处输入图片描述

原则上,这也可以避免\getcurrentref命令。

答案2

有这样的事吗?

\documentclass[a4paper, 11pt]{article}
\usepackage[ngerman]{babel}
\usepackage[utf8x]{inputenc}
\usepackage{hyperref}


\newcounter{book}
\newcounter{subbook}[book]

\def\bookagraph#1{\stepcounter{book}\stepcounter{subbook} \paragraph{B \arabic{book} -\arabic{subbook} #1}\expandafter\label{bookagraph_\arabic{book}\arabic{subbook}}}

\begin{document}

\arabic{book}

\bookagraph{Heute}

As seen in \nameref{bookagraph_11}

\end{document}

如有其他愿望,请询问。

相关内容