我有兴趣让几个页码引用同一页吗?
下面是一个例子来说明我所拥有的
\documentclass{article}
\begin{document}
\tableofcontents
\setcounter{page}{3}
\subsection{Introduction}
\newpage
\setcounter{page}{5} % what I would like to be able to write {4/5}
\subsection{Something}
\newpage
\setcounter{page}{7}
\subsection{Conclusion}
\end{document}
我想要做的是,例如设置\setcounter{page}{4/5}
和获取类似的东西,
也可以选择设置\setcounter{page}{5-8}
我使用此代码生成了上述内容(然后删除了一些页面)
\documentclass{article}
\usepackage[a5paper]{geometry}
\usepackage{lipsum}
\renewcommand*{\thepage}{\Huge\arabic{page}}
% 1, 2, 3/4, 5-8, and 9
\begin{document}
{\Huge \tableofcontents}
\setcounter{page}{1}
\subsection{Introduction}
\lipsum[1]
\newpage
\setcounter{page}{2}
\subsection{Foo}
\lipsum[1-3]
\newpage
\setcounter{page}{3} % here I would like to be able to write \setcounter{page}{3/4}
\subsection{Something}
\lipsum[1-3]
\newpage
4
\newpage
\setcounter{page}{5} % here I would like to be able to write \setcounter{page}{5-8}
\subsection{Bar}
\lipsum[1-3]
\newpage
6
\newpage
7
\newpage
9
\newpage
\subsection{Conclusion}
\lipsum[1-3]
\end{document}
答案1
使用该包,everypage
您可以创建一个宏来指定自定义页码,该页码在页面结束后自动重置:
\documentclass{article}
\usepackage[a5paper]{geometry}
\usepackage{lipsum}
%% Specify the default page format:
\newcommand*{\thepageformat}{\arabic{page}}
\renewcommand*{\thepage}{\Huge\thepageformat}
%% Automatically reset the page format to default on every page:
\usepackage{everypage}
\AddEverypageHook{
\global\def\thepageformat{\arabic{page}}
}
%% A macro to set the page "number" manually for the current page:
\newcommand*{\custompage}[2][1]{
%% use the optional argument to specify the number of pages in your range
\AddThispageHook{\global\def\thepageformat{#2}\addtocounter{page}{-1}\addtocounter{page}{#1}}
}
\begin{document}
{\Huge \tableofcontents}
\subsection{Introduction}
\lipsum[1]
\newpage
\subsection{Foo}
\lipsum[1-3]
\newpage
\custompage[2]{3/4} %% range 3-4 (2 pages)
\subsection{Something}
\lipsum[1-3]
\newpage
\custompage[4]{5--8} %% range 5-8 (4 pages)
\subsection{Bar}
\lipsum[1-3]
\newpage
\subsection{Conclusion}
\lipsum[1-3]
\end{document}
更新:现在目录也是正确的。
编辑: 使用宏
\newcommand*{\custompagetoc}[3][1]{ %% Usage: \custompagetoc[numpages]{page number}{toc page number}
\AddThispageHook{
\global\def\thepageformat{#3} %% for the toc
\def\thepageformat{#2} %% for the page
\addtocounter{page}{-1}\addtocounter{page}{#1}
}
}
您还可以在目录中使用与页面本身不同的页码:
\custompagetoc[4]{5--8}{5+} %% on the page: "5–8", in the toc: "5+", advance counter by 4 pages instead of 1
答案2
您无法将计数器设置为某个范围。但您可以更改页码的表示形式(我保留了示例中的 \Huge)
\documentclass{article}
\usepackage[a5paper]{geometry}
\usepackage{lipsum}
\renewcommand*{\thepage}{\Huge\arabic{page}}
% 1, 2, 3/4, 5-8, and 9
\begin{document}
{\Huge \tableofcontents}
%\setcounter{page}{1}
\subsection{Introduction}
\lipsum[1]
\newpage
%\setcounter{page}{2}
\subsection{Foo}
\lipsum[1-3]
\newpage
\renewcommand\thepage{\Huge3/4}
\subsection{Something}
\lipsum[1-3]
\newpage
\renewcommand\thepage{\Huge\arabic{page}}
4
\newpage
\renewcommand\thepage{\Huge5-8}
\subsection{Bar}
\lipsum[1-3]
\newpage
\renewcommand\thepage{\Huge\arabic{page}}
6
\newpage
7
\newpage
9
\newpage
\subsection{Conclusion}
\lipsum[1-3]
\end{document}
答案3
我建议使用一个命令\newVirtualPage
(无参数),该命令可以像上面一样使用,\newpage
但除了影响页码外没有任何视觉效果。这样就无需对页码进行硬编码,也避免了可能出现的不一致。
\documentclass{article}
\newcounter{NumberVirtualPages}
% use this to create a new virtual page
% use it several times if you want several virtual pages
% the page number is adjusted automatically
\newcommand{\newVirtualPage}{%
\stepcounter{NumberVirtualPages}%
\stepcounter{page}%
}
% format to be used for page numbers
\newcommand*{\thePageFormat}{\arabic{page}}
% initialize \theRealPage
\xdef\theRealPage{\thePageFormat}
% set \pagenumber at every page depending on NumberVirtualPages
\usepackage{everypage}
\AddEverypageHook{%
% define \pagenumber
\xdef\thepage{%
\ifnum 0=\theNumberVirtualPages\relax
\theRealPage
\else \ifnum 1=\theNumberVirtualPages\relax
\theRealPage/\thePageFormat%
\else
\theRealPage--\thePageFormat%
\fi \fi
}%
% set \theRealPage to next page number
\addtocounter{page}{+1}%
\xdef\theRealPage{\thePageFormat}%
\addtocounter{page}{-1}%
% reset NumberVirtualPages
\setcounter{NumberVirtualPages}{0}%
}
% ========== TEST ==========
\usepackage{lipsum}
\begin{document}
\tableofcontents
\section{Introduction}
\label{intro}%
\lipsum[1]
\newpage
\section{Foo}
\label{foo}%
\lipsum[1-3]
\newpage
\section{Bar}
\begin{minipage}{.45\linewidth}
\label{bar-1}%
\lipsum[1]
\end{minipage}%
\hfill
\newVirtualPage
\begin{minipage}{.45\linewidth}
\label{bar-2}%
\lipsum[1]
\end{minipage}%
\newpage
\section{Another Section}
\label{a-1}%
\lipsum[1]
\newVirtualPage
\bigskip
\noindent
\begin{minipage}{.45\linewidth}
\label{a-2}%
\lipsum[1-1]
\end{minipage}%
\hfill
\newVirtualPage
\begin{minipage}{.45\linewidth}
\label{a-3}%
\lipsum[1]
\end{minipage}%
\newpage
\section{Conclusion}
\label{end}
\newcommand{\testref}[1]{section~\ref{#1} on page~\pageref{#1}}
It started with \testref{intro}, followed by \testref{foo}.
It got interesting in \testref{bar-1} and \testref{bar-2}.
As shown in \testref{a-1}, \testref{a-2} and \testref{a-3} the page number looks different when having more virtual pages.
In the end it is even possible to reference \testref{end}.
\end{document}