You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

LaTeX部分页面不显示自定义Footer页码问题求助

问题分析与解决方案

核心问题

  • 手动调用\Footer仅在指定页面生效,无法自动应用到所有页面
  • 页码被强制清空,导致\ifodd\thepage奇偶判断失效
  • fancyhdr与自定义\Footer的逻辑冲突
  • 重复重定义\tableofcontents造成代码冗余

修复后的完整代码

\documentclass[a4paper,12pt,fleqn]{book}
\usepackage[utf8]{inputenc}
\usepackage{tikz}
\usepackage[explicit]{titlesec}
\usepackage{xcolor}
\usepackage[tmargin=2cm,rmargin=2.8cm,lmargin=2.3cm,bmargin=2cm]{geometry}
\usepackage{titletoc}
\usepackage{fancyhdr}
\usepackage{eso-pic}
\usepackage{lipsum}

% 定义颜色
\definecolor{col1}{RGB}{243,111,36}
\definecolor{col2}{RGB}{0,182,187}
\definecolor{col3}{RGB}{240,225,225}
\definecolor{col4}{RGB}{221,10,138}
\definecolor{col5}{RGB}{0,182,189}

% 章节标题样式
\titleformat{\section}[display] 
{\bfseries\color{col1}}
{
    \begin{tikzpicture} 
        \fill[black, rounded corners=10] (0,0.9)--(-1.4,0)--(0,-0.8)--cycle;
        \fill[rounded corners=10,col1!80,xshift=-0.5mm,yshift=-1mm](0,0.9)--(-1.4,0)--(0,-0.8)--cycle;
        \path[fill=black, draw=black!10, thick, xshift=-0.5mm, yshift=-0.5mm] (0,0) circle(0.5cm);
        \path[fill=white,draw=black!10,thick](0,0)circle(0.5cm)node[color=black](num){\thesection};
        \node[color=black,minimum height=2cm,minimum width=15cm,text width=10cm,align=left] (tit)at([xshift=7cm]num)[align=left,scale=1.2]{#1};
    \end{tikzpicture}
}{-25pt}{\bfseries\large}

% 页眉页脚基础设置
\pagestyle{fancy}
\fancyhf{} % 清空默认页眉页脚
\renewcommand{\headrulewidth}{0pt} % 隐藏页眉线
\renewcommand{\footrulewidth}{0pt} % 隐藏页脚线

% 自定义页脚命令
\newcommand\Footer{%
    \begin{tikzpicture}[remember picture, overlay]
        \ifodd\value{page} % 使用value{page}避免格式影响判断
            % 奇数页:右下角页码
            \node[anchor=south east, inner sep=20pt, outer sep=10pt] at (current page.south east) {
                \colorbox{col1}{\parbox[0.5cm][0.5cm][c]{2.5cm}{\centering\textcolor{white}{{\sffamily\bfseries\thepage}}}}
            };
        \else
            % 偶数页:左下角页码
            \node[anchor=south west, inner sep=20pt, outer sep=10pt] at (current page.south west) {
                \colorbox{col1}{\parbox[0.5cm][0.5cm][c]{2.5cm}{\centering\textcolor{white}{{\sffamily\bfseries\thepage}}}}
            };
        \fi
    \end{tikzpicture}%
}

% 使用eso-pic自动将页脚添加到所有页面
\AddToShipoutPictureBG{%
    \Footer
}

% 目录样式定义
\makeatletter
\renewcommand{\tableofcontents}{%
    \chapter*{%
        \vspace*{10\p@}%
        \begin{tikzpicture}[remember picture, overlay]%
            \draw[fill=col2,draw=col2] (10,-.75) rectangle (40,1);%
            \pgftext[right,x=15cm,y=0.2cm]{\color{white}\Huge\sc\bfseries \contentsname};%
        \end{tikzpicture}}%
    \@starttoc{toc}%
    \thispagestyle{empty} % 目录页不显示页码
}

\titlecontents{chapter}[3.8pc]
{\addvspace{30pt}%
    \begin{tikzpicture}[remember picture, overlay]%
        \draw[fill=col1,draw=col1] (-100,-.11) rectangle (-0.1,.6);%
        \pgftext[left,x=-3.5cm,y=0.2cm]{\color{white}\Large\sc\bfseries Capitolul\ \thecontentslabel};%
    \end{tikzpicture}\color{col1}\large\sc\bfseries}%
{}
{}
{\;\titlerule\;\large\sc\bfseries Pagina \thecontentspage
    \begin{tikzpicture}[remember picture, overlay]
        \draw[fill=col1!25,draw=col1!20] (2pt,0) rectangle (4,0.1pt);
\end{tikzpicture}}%

\titlecontents{section}[2.4pc]
    {\addvspace{1pt}}
    {\contentslabel[\thecontentslabel]{2.4pc}}
    {}
    {\hfill\small \thecontentspage}
    []

\titlecontents{subsection}[4.5pc] 
    {\addvspace{1pt}}
    {\contentslabel[\thecontentslabel]{2.4pc}}
    {}
    {\hfill\small \thecontentspage}
    []
\makeatother

\begin{document}
    \frontmatter % 前置部分使用罗马页码
    \tableofcontents

    \mainmatter % 主体部分使用阿拉伯页码

    \part{Recapitularea  materiei din clasele IX-X}
    \thispagestyle{empty} % 分标题页不显示页码

    \chapter{Operații cu numere reale}
    \thispagestyle{empty} % 章首页不显示页码
    
    \section{section1}
    \lipsum[10-50]
    \section{section2}
    \lipsum[10-30]

    \chapter{Functii}
    \thispagestyle{empty} % 章首页不显示页码
    \lipsum[1-20]
    
\end{document}

关键修改说明

  1. 恢复页码核心功能:删除\renewcommand{\thepage}{}和\pagenumbering{gobble},改用fancyhdr清空默认页眉页脚,保留\thepage数值用于奇偶页判断。
  2. 自动应用页脚:使用eso-pic的\AddToShipoutPictureBG命令,将\Footer自动添加到所有页面背景层,无需手动调用。
  3. 优化奇偶页判断:将\ifodd\thepage改为\ifodd\value{page},避免页码格式(如罗马数字)影响判断逻辑。
  4. 清理冗余定义:删除重复的\tableofcontents重定义,保留完整的目录标题样式。
  5. 统一页面样式控制:对分标题页、章首页、目录页使用\thispagestyle{empty}隐藏页码,其他页面自动显示自定义页脚。
  6. 修复代码小错误:修正\fill命令中的多余逗号(原代码\fill[,rounded corners=10,...])。

内容的提问来源于stack exchange,提问作者PBiux

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.16 18:07:05