latexで現在のページ/総ページ表記をさせたい

概要

院の研究計画書(14ページ参照)現在のページ数/総ページ数てな感じのフォーマットを指定されてた.
苦労したのでメモ.

環境

  • Pop!_OS 20.10
  • texlive 2020

texファイルの編集

プリアンブルに \usepackage{myfancyhdr}\usepackage{lastpage}, \pagestyle{fancy}, \cfoot{\thepage{}/{}\pageref{LastPage}}を追加する.
こんな感じ.

\documentclass[a4j,10pt, twocolumn]{jarticle}
\usepackage[dvipdfmx]{graphicx}
\usepackage{myfancyhdr}
\usepackage{amssymb}
% \usepackage{amsmath}
\usepackage{float}
\usepackage{usebib}
\bibinput{refs}
\usepackage{lastpage}
% \usepackage{slashbox}
%---------------------------------------------------
% ページの設定
%---------------------------------------------------
\setlength{\textwidth}{170truemm}
\setlength{\textheight}{250truemm}
\setlength{\topmargin}{-14.5truemm}
\setlength{\oddsidemargin}{-5.5truemm}
\pagestyle{fancy}
\cfoot{\thepage{}/{}\pageref{LastPage}}
\setlength{\headheight}{0truemm}
\setlength{\parindent}{1zw}

ちなみに使ったテンプレートはこれです.

styファイルを用意する

自分の環境だと

/usr/share/texlive/texmf-dist/tex/latex/fancyhdr/fancyhdr.sty

に目的のstyファイルがあった.

$kpsewhich fancyhdr.sty

でインストールされてるパスが分かる. こいつをcatしてコピペするなりcpするなりして作業ディレクトリに持っていきmyfancyhdr.styとして保存.

公式リポジトリから落としてきてもいいがバージョンが変わってるはずので適宜読み替えるか, 下に貼ったやつを丸コピしてください.

23行目を

\ProvidesPackage{fancyhdr}%

から

\ProvidesPackage{myfancyhdr}%

に変更する.

次に146-152行目と181行目をコメントアウトする. 181行目はコメントアウトしなくても大丈夫だけど, しなかったらページの上部に謎の横線が入る. 説明ダルいからどんな風になるかはコンパイルして確認してみてください. とりあえずここを消すと単にページ番号の表記が目的通りになる.

以下編集後のファイル全文

%%
%% This is file `fancyhdr.sty',
%% generated with the docstrip utility.
%%
%% The original source files were:
%%
%% fancyhdr.dtx  (with options: `fancyhdr')
%% 
%% This is a generated file.
%% 
%% This file may be distributed and/or modified under the conditions of
%% the LaTeX Project Public License, either version 1.3 of this license
%% or (at your option) any later version.  The latest version of this
%% license is in:
%% 
%%    http://www.latex-project.org/lppl.txt
%% 
%% and version 1.3 or later is part of all distributions of LaTeX version
%% 2005/12/01 or later.
%% 
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\NeedsTeXFormat{LaTeX2e}
\ProvidesPackage{myfancyhdr}%
           [2019/01/31 v3.10
                  Extensive control of page headers and footers]%
% Copyright (C) 1994-2019 by Piet van Oostrum <piet@vanoostrum.org>
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\def\if@nch@mpty#1{\def\temp@a{#1}\ifx\temp@a\@empty}
\def\f@nch@def#1#2{\if@nch@mpty{#2}\f@nch@gbl\def#1{\leavevmode}\else
                                   \f@nch@gbl\def#1{#2\strut}\fi}
\let\f@nch@gbl\global
\newcommand{\f@nch@ifundefined}[1]{%
  \begingroup\expandafter\expandafter\expandafter\endgroup
  \expandafter\ifx\csname #1\endcsname\relax
    \expandafter\@firstoftwo
  \else
    \expandafter\@secondoftwo
    \fi}
\newcommand\f@nch@errmsg[1]{%
  \f@nch@ifundefined{PackageError}{\errmessage{#1}}{\PackageError{Fancyhdr}{#1}{}}}
\newcommand\f@nch@warning[1]{%
  \f@nch@ifundefined{PackageWarning}{\errmessage{#1}}{\PackageWarning{Fancyhdr}{#1}{}}}
\newcommand{\f@nch@forc}[3]{\expandafter\f@rc\expandafter#1\expandafter{#2}{#3}}
\newcommand{\f@rc}[3]{\def\temp@ty{#2}\ifx\@empty\temp@ty\else
                                    \f@nch@rc#1#2\f@nch@rc{#3}\fi}
\def\f@nch@rc#1#2#3\f@nch@rc#4{\def#1{#2}#4\f@rc#1{#3}{#4}}
\newcommand{\f@nch@for}[3]{\edef\@fortmp{#2}%
  \expandafter\@forloop#2,\@nil,\@nil\@@#1{#3}}
\newcommand\f@nch@default[3]{%
  \edef\temp@a{\lowercase{\edef\noexpand\temp@a{#3}}}\temp@a \def#1{}%
  \f@nch@forc\tmpf@ra{#2}%
  {\expandafter\f@nch@ifin\tmpf@ra\temp@a{\edef#1{#1\tmpf@ra}}{}}%
  \ifx\@empty#1\def#1{#2}\fi}
\newcommand{\f@nch@ifin}[4]{%
  \edef\temp@a{#2}\def\temp@b##1#1##2\temp@b{\def\temp@b{##1}}%
  \expandafter\temp@b#2#1\temp@b\ifx\temp@a\temp@b #4\else #3\fi}
\newcommand{\fancyhead}[2][]{\f@nch@fancyhf\fancyhead h[#1]{#2}}%
\newcommand{\fancyfoot}[2][]{\f@nch@fancyhf\fancyfoot f[#1]{#2}}%
\newcommand{\fancyhf}[2][]{\f@nch@fancyhf\fancyhf {}[#1]{#2}}%
\newcommand{\fancyheadoffset}[2][]{\f@nch@fancyhfoffs\fancyheadoffset h[#1]{#2}}%
\newcommand{\fancyfootoffset}[2][]{\f@nch@fancyhfoffs\fancyfootoffset f[#1]{#2}}%
\newcommand{\fancyhfoffset}[2][]{\f@nch@fancyhfoffs\fancyhfoffset {}[#1]{#2}}%
\def\f@nch@fancyhf#1#2[#3]#4{%
  \def\temp@c{}%
  \f@nch@forc\tmpf@ra{#3}%
  {\expandafter\f@nch@ifin\tmpf@ra{eolcrhf,EOLCRHF}%
    {}{\edef\temp@c{\temp@c\tmpf@ra}}}%
  \ifx\@empty\temp@c\else \f@nch@errmsg{Illegal char `\temp@c' in
    \string#1 argument: [#3]}%
  \fi \f@nch@for\temp@c{#3}%
  {\f@nch@default\f@nch@@eo{eo}\temp@c \if@twoside\else \if\f@nch@@eo
    e\f@nch@warning {\string#1's `E' option without twoside option is
      useless}\fi\fi \f@nch@default\f@nch@@lcr{lcr}\temp@c
    \f@nch@default\f@nch@@hf{hf}{#2\temp@c}%
    \f@nch@forc\f@nch@eo\f@nch@@eo
        {\f@nch@forc\f@nch@lcr\f@nch@@lcr
          {\f@nch@forc\f@nch@hf\f@nch@@hf
            {\expandafter\f@nch@def\csname
              f@nch@\f@nch@eo\f@nch@lcr\f@nch@hf\endcsname {#4}}}}}}
\def\f@nch@fancyhfoffs#1#2[#3]#4{%
  \def\temp@c{}%
  \f@nch@forc\tmpf@ra{#3}%
  {\expandafter\f@nch@ifin\tmpf@ra{eolrhf,EOLRHF}%
    {}{\edef\temp@c{\temp@c\tmpf@ra}}}%
  \ifx\@empty\temp@c\else \f@nch@errmsg{Illegal char `\temp@c' in
    \string#1 argument: [#3]}%
  \fi \f@nch@for\temp@c{#3}%
  {\f@nch@default\f@nch@@eo{eo}\temp@c \if@twoside\else \if\f@nch@@eo
    e\f@nch@warning {\string#1's `E' option without twoside option is
      useless}\fi\fi \f@nch@default\f@nch@@lcr{lr}\temp@c
    \f@nch@default\f@nch@@hf{hf}{#2\temp@c}%
    \f@nch@forc\f@nch@eo\f@nch@@eo
        {\f@nch@forc\f@nch@lcr\f@nch@@lcr
          {\f@nch@forc\f@nch@hf\f@nch@@hf
            {\expandafter\setlength\csname
              f@nch@O@\f@nch@eo\f@nch@lcr\f@nch@hf\endcsname {#4}}}}}%
  \f@nch@setoffs}
\newcommand{\lhead}[2][\f@nch@olh]%
                     {\f@nch@def\f@nch@olh{#2}\f@nch@def\f@nch@elh{#1}}
\newcommand{\chead}[2][\f@nch@och]%
                     {\f@nch@def\f@nch@och{#2}\f@nch@def\f@nch@ech{#1}}
\newcommand{\rhead}[2][\f@nch@orh]%
                     {\f@nch@def\f@nch@orh{#2}\f@nch@def\f@nch@erh{#1}}
\newcommand{\lfoot}[2][\f@nch@olf]%
                     {\f@nch@def\f@nch@olf{#2}\f@nch@def\f@nch@elf{#1}}
\newcommand{\cfoot}[2][\f@nch@ocf]%
                     {\f@nch@def\f@nch@ocf{#2}\f@nch@def\f@nch@ecf{#1}}
\newcommand{\rfoot}[2][\f@nch@orf]%
                     {\f@nch@def\f@nch@orf{#2}\f@nch@def\f@nch@erf{#1}}
\newlength{\f@nch@headwidth} \let\headwidth\f@nch@headwidth
\newlength{\f@nch@O@elh}
\newlength{\f@nch@O@erh}
\newlength{\f@nch@O@olh}
\newlength{\f@nch@O@orh}
\newlength{\f@nch@O@elf}
\newlength{\f@nch@O@erf}
\newlength{\f@nch@O@olf}
\newlength{\f@nch@O@orf}
\newcommand{\headrulewidth}{0.4pt}
\newcommand{\footrulewidth}{0pt}
\f@nch@ifundefined{footruleskip}%
      {\newcommand{\footruleskip}{.3\normalbaselineskip}}{}
\newcommand{\plainheadrulewidth}{0pt}
\newcommand{\plainfootrulewidth}{0pt}
\newif\if@fancyplain \@fancyplainfalse
\def\fancyplain#1#2{\if@fancyplain#1\else#2\fi}
\headwidth=-123456789sp
\let\f@nch@raggedleft\raggedleft
\let\f@nch@raggedright\raggedright
\let\f@nch@centering\centering
\let\f@nch@everypar\everypar
\def\f@nch@reset{\f@nch@everypar{}\restorecr\endlinechar=13
  \let\\\@normalcr \let\raggedleft\f@nch@raggedleft
  \let\raggedright\f@nch@raggedright \let\centering\f@nch@centering
  \def\baselinestretch{1}%
  \hsize=\headwidth
  \def\nouppercase##1{{\let\uppercase\relax\let\MakeUppercase\relax
      \expandafter\let\csname MakeUppercase \endcsname\relax##1}}%
  \f@nch@ifundefined{@newbaseline} % NFSS not present; 2.09 or 2e
  {\f@nch@ifundefined{@normalsize} {\normalsize} % for ucthesis.cls
   {\@normalsize}}%
  {\@newbaseline}% NFSS (2.09) present
  }
\fancyhf{}
% Lines 146-152 were commented out by sioremon on May 14, 2021
% \if@twoside
%   \fancyhead[el,or]{\fancyplain{}{\slshape\rightmark}}
%   \fancyhead[er,ol]{\fancyplain{}{\slshape\leftmark}}
% \else
%   \fancyhead[l]{\fancyplain{}{\slshape\rightmark}}
%   \fancyhead[r]{\fancyplain{}{\slshape\leftmark}}
% \fi
\fancyfoot[c]{\rmfamily\thepage} % page number
\newcommand\f@nch@vbox[2]{%
  \setbox0\vbox{#2}%
  \ifdim\ht0>#1\relax
    \f@nch@warning{%
      \string#1 is too small (\the#1): ^^J%
      Make it at least \the\ht0.^^J%
      We now make it that large for the rest of the document.^^J%
      This may cause the page layout to be inconsistent, however\@gobble
    }%
    \dimen0=#1\relax
    \global#1=\ht0\relax
    \ht0=\dimen0 %
  \fi
  \box0}
\newcommand\f@nch@head[5]{%
  #1%
  \hbox to\headwidth{%
    \f@nch@reset
    \f@nch@vbox\headheight{%
      \hbox{%
        \rlap{\parbox[b]{\headwidth}{\raggedright#2}}%
        \hfill
        \parbox[b]{\headwidth}{\centering#3}%
        \hfill
        \llap{\parbox[b]{\headwidth}{\raggedleft#4}}%
      }%
      % Lines 181 were commented out by sioremon on May 14, 2021
      % \headrule
    }%
  }%
  #5%
}
\newcommand\f@nch@foot[5]{%
  #1%
  \hbox to\headwidth{%
    \f@nch@reset
    \f@nch@vbox\footskip{%
      \setbox0=\vbox{\footrule}\unvbox0
      \vskip\footruleskip
      \hbox{%
        \rlap{\parbox[t]{\headwidth}{\raggedright#2}}%
        \hfill
        \parbox[t]{\headwidth}{\centering#3}%
        \hfill
        \llap{\parbox[t]{\headwidth}{\raggedleft#4}}%
      }%
    }%
  }%
  #5%
}
\def\headrule{{\if@fancyplain\let\headrulewidth\plainheadrulewidth\fi
    \hrule\@height\headrulewidth\@width\headwidth
    \vskip-\headrulewidth}}
\def\footrule{{\if@fancyplain\let\footrulewidth\plainfootrulewidth\fi
    \hrule\@width\headwidth\@height\footrulewidth}}
\def\ps@fancy{%
  \f@nch@ifundefined{@chapapp}{\let\@chapapp\chaptername}{}% for amsbook
\f@nch@ifundefined{MakeUppercase}{\def\MakeUppercase{\uppercase}}{}%
\f@nch@ifundefined{chapter}%
 {\def\sectionmark##1{\markboth{\MakeUppercase{\ifnum \c@secnumdepth>\z@
        \thesection\hskip 1em\relax
      \fi ##1}}{}}%
  \def\subsectionmark##1{\markright {\ifnum \c@secnumdepth >\@ne
    \thesubsection\hskip 1em\relax \fi ##1}}}%
 {\def\chaptermark##1{\markboth {\MakeUppercase{\ifnum
      \c@secnumdepth>\m@ne \@chapapp\ \thechapter. \ \fi ##1}}{}}%
\def\sectionmark##1{\markright{\MakeUppercase{\ifnum \c@secnumdepth >\z@
      \thesection. \ \fi ##1}}}%
}
\ps@@fancy
\gdef\ps@fancy{\@fancyplainfalse\ps@@fancy}%
\ifdim\headwidth<0sp
    \global\advance\headwidth123456789sp\global\advance\headwidth\textwidth
\fi}
\def\ps@fancyplain{\ps@fancy \let\ps@plain\ps@plain@fancy}
\def\ps@plain@fancy{\@fancyplaintrue\ps@@fancy}
\let\ps@@empty\ps@empty
\def\ps@@fancy{%
  \ps@@empty
  \def\@mkboth{\protect\markboth}%
  \def\@oddhead{\f@nch@head\f@nch@Oolh\f@nch@olh\f@nch@och\f@nch@orh\f@nch@Oorh}%
  \def\@oddfoot{\f@nch@foot\f@nch@Oolf\f@nch@olf\f@nch@ocf\f@nch@orf\f@nch@Oorf}%
  \def\@evenhead{\f@nch@head\f@nch@Oelh\f@nch@elh\f@nch@ech\f@nch@erh\f@nch@Oerh}%
  \def\@evenfoot{\f@nch@foot\f@nch@Oelf\f@nch@elf\f@nch@ecf\f@nch@erf\f@nch@Oerf}%
}
\def\f@nch@Oolh{\if@reversemargin\hss\else\relax\fi}
\def\f@nch@Oorh{\if@reversemargin\relax\else\hss\fi}
\let\f@nch@Oelh\f@nch@Oorh
\let\f@nch@Oerh\f@nch@Oolh
\let\f@nch@Oolf\f@nch@Oolh
\let\f@nch@Oorf\f@nch@Oorh
\let\f@nch@Oelf\f@nch@Oelh
\let\f@nch@Oerf\f@nch@Oerh
\def\f@nch@offsolh{\headwidth=\textwidth\advance\headwidth\f@nch@O@olh
                   \advance\headwidth\f@nch@O@orh\hskip-\f@nch@O@olh}
\def\f@nch@offselh{\headwidth=\textwidth\advance\headwidth\f@nch@O@elh
                   \advance\headwidth\f@nch@O@erh\hskip-\f@nch@O@elh}
\def\f@nch@offsolf{\headwidth=\textwidth\advance\headwidth\f@nch@O@olf
                   \advance\headwidth\f@nch@O@orf\hskip-\f@nch@O@olf}
\def\f@nch@offself{\headwidth=\textwidth\advance\headwidth\f@nch@O@elf
                   \advance\headwidth\f@nch@O@erf\hskip-\f@nch@O@elf}
\def\f@nch@setoffs{%
  \f@nch@gbl\let\headwidth\f@nch@headwidth
  \f@nch@gbl\let\f@nch@Oolh\f@nch@offsolh
  \f@nch@gbl\let\f@nch@Oelh\f@nch@offselh \f@nch@gbl\let\f@nch@Oorh\hss
  \f@nch@gbl\let\f@nch@Oerh\hss \f@nch@gbl\let\f@nch@Oolf\f@nch@offsolf
  \f@nch@gbl\let\f@nch@Oelf\f@nch@offself \f@nch@gbl\let\f@nch@Oorf\hss
  \f@nch@gbl\let\f@nch@Oerf\hss
}
\newif\iff@nch@footnote
\AtBeginDocument{%
  \let\latex@makecol\@makecol
  \def\@makecol{\ifvoid\footins\f@nch@footnotefalse\else\f@nch@footnotetrue\fi
    \let\topfloat\@toplist\let\botfloat\@botlist\latex@makecol}%
}
\newcommand\iftopfloat[2]{\ifx\topfloat\empty #2\else #1\fi}%
\newcommand\ifbotfloat[2]{\ifx\botfloat\empty #2\else #1\fi}%
\newcommand\iffloatpage[2]{\if@fcolmade #1\else #2\fi}%
\newcommand\iffootnote[2]{\iff@nch@footnote #1\else #2\fi}%
\newcommand{\fancypagestyle}[2]{%
  \@namedef{ps@#1}{\let\f@nch@gbl\relax#2\relax\ps@fancy}}
\endinput
%%
%% End of file `fancyhdr.sty'.

雰囲気でlatexを触っているのでstyファイルだのclsファイルってのが何のファイルかイマイチ分かっとらん. なんか間違いがあればコメントで教えてください.

参考文献

zenn.dev

osksn2.hep.sci.osaka-u.ac.jp

tex.stackexchange.com

https://www.y-misc.org/tex/template/twocolumn.tex