LaTeX article类文档全页水印及透明度控制问题求助
LaTeX article类文档全页水印及透明度控制问题求助
嘿,我来帮你搞定这两个头疼的问题!你遇到的水印只显示在第一页、没法调整透明度的情况,大概率是xwatermark和你用的fancyhdr页面样式存在兼容性冲突,而且xwatermark的透明度参数在某些场景下需要额外配置才能生效。下面给你两种靠谱的解决方案,你可以按需选:
方案一:修复xwatermark的使用问题
首先要调整xwatermark的加载顺序,再优化参数设置。把它放在页面样式配置之后加载,同时用更稳妥的方式控制透明度:
\documentclass[12pt,a4paper,twoside,envcountsect]{article} \usepackage{fancyhdr} \usepackage{lastpage} \usepackage[titles,subfigure]{tocloft} \usepackage{subcaption} \usepackage{geometry} \geometry{top=2.5cm , bottom=2.5cm , left=2.5cm , right=2.5cm , headsep=9mm , footskip=14mm , marginparwidth=2.0cm , marginparsep=0.25cm , a4paper , pdftex} \pagestyle{fancy} \fancyhead{} \fancyhead[LE,RO]{ \footnotesize \nouppercase \leftmark } \fancyhead[LE,RO]{} \lhead{\footnotesize lhead} \chead{\footnotesize chead} \rhead{\footnotesize rhead} \fancyfoot{} \rfoot{Page \thepage \space of \pageref{LastPage}} \lfoot{lfoot} \renewcommand{\headrulewidth}{0.6pt} \renewcommand{\footrulewidth}{0.6pt} \renewcommand{\sectionmark}[1]{\markboth{#1}{}} % 把xwatermark放在页面样式设置之后加载,确保绑定生效 \usepackage[printwatermark]{xwatermark} \newwatermark[ allpages, opacity=0.2, % 取值范围0-1,数值越小越透明 color=red!40, % 也可以通过颜色淡色值间接控制透明度,和opacity叠加效果更灵活 angle=45, scale=3.14, xpos=0, ypos=0 ]{WATERMARK} \usepackage[]{hyperref} \newcounter{Chapcounter} \newcommand\showmycounter{\addtocounter{Chapcounter}{1}\themycounter} \newcommand{\chapter}[1] { \addcontentsline{toc}{section}{\large\color{cyan} #1} } \hypersetup{colorlinks=true} \hypersetup{linkcolor=black} \begin{document} \thispagestyle{fancy} % 强制第一页使用fancy样式,避免默认plain样式导致水印失效 \renewcommand*{\contentsname}{\LARGE Contents} \vspace{1cm} \begin{center} \huge{Headline} \end{center} \vspace{1cm} \tableofcontents\newpage \chapter{Calculus} \section{Subject} \newpage \chapter{Algebra} \section{Subject} \end{document}
方案二:改用background包(兼容性更强)
如果觉得xwatermark的坑太多,推荐用background包,它对各类页面样式的兼容性更好,透明度控制也更直观,默认就会在所有页面添加水印:
\documentclass[12pt,a4paper,twoside,envcountsect]{article} \usepackage{fancyhdr} \usepackage{lastpage} \usepackage[titles,subfigure]{tocloft} \usepackage{subcaption} \usepackage{geometry} % 引入background包 \usepackage{background} \geometry{top=2.5cm , bottom=2.5cm , left=2.5cm , right=2.5cm , headsep=9mm , footskip=14mm , marginparwidth=2.0cm , marginparsep=0.25cm , a4paper , pdftex} \pagestyle{fancy} \fancyhead{} \fancyhead[LE,RO]{ \footnotesize \nouppercase \leftmark } \fancyhead[LE,RO]{} \lhead{\footnotesize lhead} \chead{\footnotesize chead} \rhead{\footnotesize rhead} \fancyfoot{} \rfoot{Page \thepage \space of \pageref{LastPage}} \lfoot{lfoot} \renewcommand{\headrulewidth}{0.6pt} \renewcommand{\footrulewidth}{0.6pt} \renewcommand{\sectionmark}[1]{\markboth{#1}{}} % 配置background水印 \backgroundsetup{ scale=3.14, angle=45, opacity=0.5, % 0-1直接控制透明度,数值越小越淡 color=red, contents={WATERMARK}, position={current page.center}, % 居中显示 vshift=0, hshift=0 } \usepackage[]{hyperref} \newcounter{Chapcounter} \newcommand\showmycounter{\addtocounter{Chapcounter}{1}\themycounter} \newcommand{\chapter}[1] { \addcontentsline{toc}{section}{\large\color{cyan} #1} } \hypersetup{colorlinks=true} \hypersetup{linkcolor=black} \begin{document} \renewcommand*{\contentsname}{\LARGE Contents} \vspace{1cm} \begin{center} \huge{Headline} \end{center} \vspace{1cm} \tableofcontents\newpage \chapter{Calculus} \section{Subject} \newpage \chapter{Algebra} \section{Subject} \end{document}
问题根源说明
- 水印仅第一页显示:LaTeX默认第一页用
plain样式,而你设置了全局fancy样式,xwatermark的allpages参数可能没覆盖到plain样式页面;另外加载顺序不对也会导致水印和页面样式绑定失败。 - 透明度无法调整:
xwatermark的opacity参数在部分环境下需要配合颜色淡色值使用,或者包版本过旧导致参数不生效,而background包的透明度控制逻辑更直接。
备注:内容来源于stack exchange,提问作者Peter Jensen
相关产品推荐
相关产品推荐

