페이지 머리글에 미리 정의된 단락 번호 삽입

페이지 머리글에 미리 정의된 단락 번호 삽입

나는 소스에 이미 정의된 "단락" 번호가 있는 번역된 텍스트를 다루고 있으며 일관성을 유지해야 합니다. 따라서 \theparagraph숫자를 생성하는 데 사용할 수 없습니다 . 복잡성을 더하기 위해 많은 단락에 숫자가 없습니다. 또한 일부 단락에는 범위(1-10 등)가 있습니다. 이러한 미리 정의된 번호는 이미 내 문서에 있습니다.

나는 미적인 이유로 단락 앞이 아닌 페이지 헤더에 이 숫자를 표시하고 싶습니다. fancyhdr을 사용하고 있습니다. 페이지 머리글에서는 두 페이지 모두에 표시되며 앞에 섹션 번호(장이 아님)가 표시됩니다. 따라서 각 페이지 헤더에는 다음이 포함됩니다.

알파벳

A는 섹션 번호이고, B는 페이지의 첫 번째 문단 번호입니다. 물론 이전 페이지의 마지막 문단과 이어지는 경우가 많습니다. C는 페이지의 마지막 문단 번호입니다. .

이는 충분히 복잡해야 하지만 추가로 범위를 처리해야 합니다. 이러한 경우 B 번호의 경우 범위의 첫 번째 숫자를 선택합니다. C 번호의 경우 마지막을 선택합니다.

따라서 페이지의 첫 번째 단락 범위가 10-14이고 마지막 단락의 범위가 16-18인 경우 해당 페이지의 헤더에는 A.10-18이 있습니다.

명확하길 바랍니다. 추가 설명이 필요하면 알려주시기 바랍니다.

몇 가지 추가 세부정보입니다. 섹션은 새 페이지에서 시작하는데, 나는 titlesec을 사용합니다. (어떤 이유로 아래에 제공하는 MWE는 실제로 시작되기 전에 섹션 2를 헤더에 넣습니다. 이에 대해서는 확인하지 않았습니다.)

단락과 같은 섹션에는 하드 코딩된 번호가 있으므로 더 쉽게 사용할 수 있습니다. 그러나 단락과 달리 섹션 번호는 단순하고 연속적이므로 LaTeX가 섹션 번호를 계산하도록 하는 것이 기본적으로 그렇게 하기를 원하기 때문에 아마도 더 간단할 것이라고 생각합니다. 하지만 어느 쪽이든 괜찮습니다.

여기 MWE가 있습니다. 이 작업을 수행하는 가장 좋은 방법은 잘 모르겠지만 MWE를 원래 예제에서 아래 Andrew의 답변을 적용한 예제로 변경했습니다. 나는 그의 대답을 대체로 옳은 것으로 받아들였습니다. 하지만 코드를 사용하면서 아래에 나열한 수많은 버그를 발견했습니다.

\documentclass{book}
\usepackage{lipsum}
\setlipsumdefault{1}
\usepackage{titlesec}

% fancy headings as defined by the OP except we need to be more careful
% with plain headings
\usepackage{fancyhdr}
\fancypagestyle{plain}{% need to reset \firstpara on pages without headers
  \fancyhf{} % clear all header and footer fields
  \renewcommand{\headrulewidth}{0pt}
  \fancyfoot[C]{\bfseries \thepage}
  \xdef\firstpara{\lastpara}
}
\pagestyle{fancy}
\fancyhf{}
\fancyhead[LE,RO]{\thepage}
\fancyhead[CO]{\rightmark}
\renewcommand{\sectionmark}[1]{%
   \markright{#1}%
   \def\firstpara{1}\def\lastpara{1}% reset counters each section
}
\fancyhead[CE]{\leftmark}
\renewcommand{\chaptermark}[1]{\markboth{#1}{}}
\fancyhead[LO,RE]{\thesection.\theparagraph}

\newcommand{\sectionbreak}{\clearpage\thispagestyle{plain}}
\renewcommand\thesection{\arabic{section}}

% now the macros for tracking the paragraph numbers
\def\firstpara{1}%    set to 1 by default
\def\lastpara{1}% set to 1 by default
\def\setparagraph#1-#2-#3|{%
   % if \firstpara=0 => new page => set \firstpara
   \ifnum\firstpara=0\def\firstpara{#1}\fi%
   % if #2=0 => only #1 is meaningful to set \lastpara to this
   % otherwise use #2
   \ifnum#2=0\def\lastpara{#1}\else\def\lastpara{#2}\fi%
}
\def\paragraph#1{%\textbf{#1}\space
  \setparagraph#1-0-|%
}
\def\theparagraph{%
  % if \firstpara=\lastpara print only \firstpara otherwise print a range
  \ifnum\firstpara=\lastpara\firstpara\else\firstpara--\lastpara\fi
  % reset firstpara and lastpara
  \xdef\firstpara{\lastpara}%
  %\let\lastpara\relax%
}

% back to the MWe
\begin{document}

\null
\pagestyle{empty}

\newpage

\newpage

\pagestyle{fancy}

\chapter{A Chapter}

\section{First Section}

\paragraph{1}\lipsum

\paragraph{2}\lipsum

\paragraph{3-5}\lipsum

\paragraph{6}\lipsum

\paragraph{7-17}\lipsum

\paragraph{18}\lipsum

\paragraph{19-20}\lipsum

\paragraph{21-28}\lipsum

\paragraph{29}\lipsum

\paragraph{30}\lipsum

\section{Second Section}

\paragraph{1}\lipsum

\paragraph{2}\lipsum

\paragraph{3-5}\lipsum

\paragraph{6}\lipsum

\paragraph{7-17}\lipsum

\paragraph{18}\lipsum

\paragraph{19-20}\lipsum

\paragraph{21-28}\lipsum

\paragraph{29}\lipsum

\paragraph{30}\lipsum

\section{Third Section}

\paragraph{1}\lipsum

\paragraph{2}\lipsum

\paragraph{3}\lipsum

\paragraph{4}\lipsum

\paragraph{5}\lipsum

\paragraph{6}\lipsum

\paragraph{7}\lipsum

\paragraph{8}\lipsum

\paragraph{9}\lipsum

\paragraph{10}\lipsum

\paragraph{11}\lipsum

\paragraph{12}\lipsum

\paragraph{13}\lipsum

\paragraph{14}\lipsum

\paragraph{15}\lipsum

\paragraph{16}\lipsum

\paragraph{17}\lipsum

\paragraph{18}\lipsum

\paragraph{19}\lipsum

\paragraph{20}\lipsum

\end{document}

제가 알고 있는 버그는 다음과 같습니다.

  1. 새 섹션이 포함된 페이지 스타일은 단순하므로 헤더가 없습니다. 이는 올바른 것입니다. 그러나 다음 페이지, 즉 헤더가 있는 섹션의 첫 번째 페이지에서는 첫 번째 단락 번호가 항상 1인 반면, 다른 모든 곳과 마찬가지로 해당 페이지의 첫 번째 단락에 대한 관련 단락 번호여야 합니다. 사실 이 버그가 MWE에 표시되도록 할 수는 없지만 전체 문서에는 표시됩니다. 1을 0으로 바꾸라는 Andrew의 제안은 \sectionmark문제를 해결하지 못했습니다. 이 버그가 표시되면 MWE를 업데이트하겠습니다.

  2. 머리글에는 일부 머리글/바닥글 자료가 표시되지만 이러한 내용은 모두 페이지 스타일이 비어 있어야 합니다. 제목 앞에 나오는 페이지에 나타납니다. MWE의 두 번째 빈 페이지를 참조하십시오.

  3. 어떤 경우에는 단락 번호 계산이 올바르지 않습니다. 이는 마지막 두 페이지의 MWE에 나타납니다. 두 번째 마지막 페이지는 16-19여야 하는데 16-20입니다. 마지막 페이지는 19-20이어야 합니다. 이것은 내 전체 문서에서 상당히 자주 발생하며 고아가 있을 때 발생하는 것으로 보입니다.

  4. 마지막 페이지에는 또 다른 사소한 버그도 표시됩니다. 한 단락의 "범위"가 있는 경우 하나만 표시되어야 합니다. 예를 들어 "20-20" 대신 "20"만 있어야 합니다.

  5. 단락 범위 처리가 지정된 대로 정확하게 이루어지지 않습니다. 예를 들어 세 번째 단락의 범위는 3-5이므로 4페이지 헤더는첫 번째이 범위의 단락은 5-20개입니다.

  6. 섹션의 마지막 헤더가 잘못된 섹션 번호를 제공합니다. 다음 섹션의 섹션 번호가 있습니다. 예를 들어 MWE의 5페이지 번호는 2.20-30이지만 첫 번째 섹션에 있으므로 1.20-30이어야 합니다.

답변1

좋은 질문! 이를 수행하는 한 가지 방법이 있습니다. 명령 을 하이재킹하여 \paragraph사용하여 주어진 인수를 인쇄 \textbf{#1}한 다음 인수로 명령 을 호출하여 단락 카운터를 설정 \firstpara합니다 .\lastpara\setparagraph#1-0-|

매크로 는 와 같은 단일 숫자 또는 와 같은 숫자 범위 \setparagraph처럼 보일 수 있는 인수를 허용해야 하기 때문에 약간 까다롭습니다 . 형식으로 제공해야 하는 세 가지 인수를 예상하여 이를 수행합니다 . 따라서 if 는 \setparagraph #1=6 #2=0 #3 7-21 \paragraph then 에 대한 인수 이고 for 입니다 .67-21#1-#2-#3|6\paragraph then the arguments forare,andis empty. Ifis the argument to#1=7#2=21#3=0\setparagaph

\firstpara그런 다음 몇 가지 게임을 플레이하여 과 같은 검사를 통해 항상 \lastpara적절한 값으로 설정되어 있는지 확인합니다 \ifnum\firstpara=0.... 제목의 단락 범위는 다음 페이지에 대해 카운터가 정확하도록 이 \theparagraph매크로도 재설정되어 인쇄됩니다.\firstpara\lastpara

다음은 몇 가지 샘플 출력입니다(명확하게 하기 위해 단락 번호 포함).

여기에 이미지 설명을 입력하세요

다음은 코드입니다(참조를 위해 단락 번호가 포함됨).

편집하다업데이트된 게시물에서 1~6을 수용하기 위해 코드를 약간 업데이트했습니다.

\documentclass{book}
\usepackage{lipsum}
\setlipsumdefault{1}
\usepackage{titlesec}
%\usepackage[hmargin=63mm,vmargin=68mm]{geometry}

% fancy headings as defined by the OP except we need to be more careful
% with plain headings
\usepackage{fancyhdr}
\fancypagestyle{empty}{
  \fancyhf{}
  \renewcommand{\headrulewidth}{0pt}
}
\fancypagestyle{plain}{
  \fancyhf{}% clear all header and footer fields
  \renewcommand{\headrulewidth}{0pt}
  \renewcommand{\footrulewidth}{0pt}
  \fancyfoot[C]{\bfseries \thepage}%
  % need to reset \firstpara on pages without headers
  \xdef\firstpara{\midpara}%
}
\fancypagestyle{paragraphs}{
  \fancyhead[LE,RO]{\thepage}
  \fancyhead[CO]{\rightmark}
  \fancyhead[CE]{\leftmark}
  \fancyhead[LO,RE]{\ifnum\firstpara>0\relax\thesection.\theparagraph\fi}
}
\renewcommand{\chaptermark}[1]{\markboth{#1}{}}
\renewcommand{\sectionmark}[1]{%
   \markright{#1}%
   % reset paragraph counters in each section
   \def\firstpara{0}\def\midpara{0}\def\lastpara{0}
}
%\newcommand{\sectionbreak}{\newpage\thispagestyle{plain}}
\let\realSection\section
\renewcommand\section[2][\relax]{\clearpage%
  \thispagestyle{plain}%
  \ifx#1\relax\relax\realSection{#2}%
  \else\realSection[#1]{#2}%
  \fi
}
\renewcommand\thesection{\arabic{section}}

% now the macros for tracking the paragraph numbers
\def\firstpara{0}%    set to 1 by default
\def\midpara{0}%    set to 1 by default
\def\lastpara{0}% set to 1 by default
\def\setparagraph#1-#2-#3|{\def\midpara{#1}%
   % if \firstpara=0 => new page => set \firstpara
   \ifnum\firstpara=0\relax\def\firstpara{#1}\fi%
   % if #2=0 => only #1 is meaningful to set \lastpara to this
   % otherwise use #2
   \ifnum#2=0\relax\def\lastpara{#1}%
   \else\def\lastpara{#2}%
   \fi%
}
\def\paragraph#1{\textbf{#1}\space
  \setparagraph#1-0-|%
}
\def\theparagraph{%
  % if \firstpara=\lastpara print only \firstpara otherwise print a range
  \ifnum\firstpara=\lastpara\relax\firstpara\else\firstpara--\lastpara\fi
  % reset firstpara and lastpara
  \xdef\firstpara{\midpara}%
  %\let\lastpara\relax%
}

% back to the MWe
\begin{document}

\null
\pagestyle{empty}

\newpage

\newpage

\pagestyle{paragraphs}

\chapter{A Chapter}

\section{First Section}

\paragraph{1}\lipsum

\paragraph{2}\lipsum

\paragraph{3-5}\lipsum

\paragraph{6}\lipsum

\paragraph{7-17}\lipsum

\paragraph{18}\lipsum

\paragraph{19-20}\lipsum

\paragraph{21-28}\lipsum

\paragraph{29}\lipsum

\paragraph{30}\lipsum

\section{Second Section}

\paragraph{1}\lipsum

\paragraph{2}\lipsum

\paragraph{3-5}\lipsum

\paragraph{6}\lipsum

\paragraph{7-17}\lipsum

\paragraph{18}\lipsum

\paragraph{19-20}\lipsum

\paragraph{21-28}\lipsum

\paragraph{29}\lipsum

\paragraph{30}\lipsum

\section{Third Section}

\paragraph{1}\lipsum

\paragraph{2}\lipsum

\paragraph{3}\lipsum

\paragraph{4}\lipsum

\paragraph{5}\lipsum

\paragraph{6}\lipsum

\paragraph{7}\lipsum

\paragraph{8}\lipsum

\paragraph{9}\lipsum

\paragraph{10}\lipsum

\paragraph{11}\lipsum

\paragraph{12}\lipsum

\paragraph{13}\lipsum

\paragraph{14}\lipsum

\paragraph{15}\lipsum

\paragraph{16}\lipsum

\paragraph{17}\lipsum

\paragraph{18}\lipsum

\paragraph{19}\lipsum

\paragraph{20}\lipsum

\end{document}

노트

  1. 문제는 2빈 페이지가 실제로 비어 있지 않기 때문에 발생했습니다. 더 정확하게 \headrulewidth는 0으로 설정되지 않았습니다 .

  2. 나는 이것을 사용하지 않으므로 fancyhdr이것이 틀릴 수도 있지만, 자신만의 멋진 페이지 스타일을 정의하는 것이 더 나을 것 같습니다. paragraph위에서 페이지 스타일을 정의했습니다 . 나는 fancy페이지 스타일이 다른 스타일과 잘 어울리지 않기 때문에 이렇게 했습니다 .

  3. \sectionbreak에서 사용한 명령이 문제 titlesec의 원인인 것 같습니다 6. 호출하기 전에 섹션 카운터를 증가시키는 것으로 의심되므로 \sectionbreak헤더가 잘못되었습니다. 나는 \sectionbreak섹션 명령을 피하고 대신 재정의하여 이 문제를 해결했습니다 .

관련 정보