我如何将图像放置在指定页面的顶部。例如,奇数页将由图像填充,甚至带有文本。或者,我只想确保 (lua)latex 将图像放置在第 2、3、6 页上?
这里讨论了类似的问题:如何在两页上添加图片,左侧部分在左侧,右侧部分在右侧(用于书籍)?
答案1
以下是解决方案的一个版本如何使用 TikZ 和 graphicx 在左上角添加图像适用于仅在页面上添加指定的徽标2,3, and 6
。它使用[some]
选项包裹background
。
要更改放置图形的页面,只需更改定义中的值即可\ListOfPagesWithLogo
。将该宏设置为:
\newcommand*{\ListOfPagesWithLogo}{2,3,6}
2
您可以在、3
和页面上获得徽标6
:
笔记:
- 我使用该
xstring
包进行数值比较,因为我喜欢这种语法,但是可以进行调整以不再需要这种语法。 - 下面的版本放置了一张图片,但如果您希望包含外部图形,
tikz
可以取消注释该行。\SetBgContents
- 需要进行两次编译运行,第一次获取坐标,第二次进行实际绘图。
代码:
% Modified version of https://tex.stackexchange.com/questions/38751/how-do-i-add-an-image-in-the-upper-left-hand-corner-using-tikz-and-graphicx/38754#38754
\documentclass{book}
\usepackage[demo]{graphicx}
\usepackage[some]{background}
\usepackage{lipsum}
\usepackage{showframe}
\usepackage{tikz}
\usepackage{xstring}
\newcommand{\MyGraphicLogo}{% For imported graphic logo
\begin{tikzpicture}[remember picture,overlay,yshift=-2cm, xshift=2cm]
\node at (0,0) {\includegraphics[width=2cm,height=2cm]{foo}};
\end{tikzpicture}
}
\newcommand{\MyTikzLogo}{% For a logo drawn with TikZ
\begin{tikzpicture}[remember picture,overlay,yshift=-1cm, xshift=1cm]
\draw [cyan,fill=yellow] (0cm,0cm)
-- (2cm, 0cm)
-- (2cm, -2cm)
-- (0cm, -2cm)
-- cycle;
\end{tikzpicture}
}
%\SetBgContents{\MyGraphicLogo}% Select included image
\SetBgContents{\MyTikzLogo}% Select tikz picture
\SetBgPosition{current page.north west}% Select location
\SetBgOpacity{1.0}% Select opacity
\SetBgAngle{0.0}% Select roation of logo
\SetBgScale{1.0}% Select scale factor of logo
\newcommand*{\ListOfPagesWithLogo}{2,3,6}%
\makeatletter
\newcommand*{\AddLogoIfCurrentPageIsOnList}{%
\foreach \LogoPage in \ListOfPagesWithLogo {%
\IfEq{\arabic{page}}{\LogoPage}{%
\bg@material%
\breakforeach%
}{}%
}%
}%
\makeatother
\AddEverypageHook{\AddLogoIfCurrentPageIsOnList}%
\begin{document}
\section*{Lorem Ipsum}
\lipsum[1-40]
\end{document}
答案2
这是一个基于 keys 的包。它不依赖背景和每一页包。
\RequirePackage{filecontents}
\begin{filecontents}{xbackground.sty}
\ProvidesPackage{xbackground}[2012/06/11 v0.0.1 Insert background stuff (AM)]
\NeedsTeXFormat{LaTeX2e}[2011/06/27]
\RequirePackage{ltxkeys}
\RequirePackage{tikz}
\RequirePackage{afterpage}
\new@def\bgdcond#1\fi{\csname @#1first\else second\fi oftwo\endcsname}
\new@def\bgdifx@aux#1#2\bgd@ifx@#3#4\bgd@ifx@@{\ifx#1#3}
\begingroup
\lccode`\&=1 \lccode`\*=2 \catcode`\&=7 \catcode`\*=8
\lowercase{\endgroup
\new@def\bgdifx#1#2{%
\csname @\bgdifx@aux#1&\bgd@ifx@#2*\bgd@ifx@@
first\else second\fi oftwo\endcsname
}
\new@def\bgdifblank#1{%
\csname @\bgdifx@aux#1&\bgd@ifx@&\bgd@ifx@@
first\else second\fi oftwo\endcsname
}
}
\cptnewvariables{box}[bgd@]{mainbox,tempbox}
\cptnewvariables{if}{bgdbreakfor,bgdlastitem,verbose}
\cptnewvariables{count}{bgdfordepth}
\cptnewvariables{if}[bgd@]{nobackground,allpages,oddpages,evenpages}
\newletcs\bgdbreakfor\bgdbreakfortrue
% Both pgf's \foreach and catoptions' \cptdocommalist are heavy macros.
% Optimosiasts can do with a lighter and nestable \bgdfor. There is a lot
% of iteration on each page.
% \bgdfor[<parser>]<holder-cmd>:={<list>}\do{<callback>}
% \bgdfor*[<parser>]<holder-cmd>:={<listcmd>}\do{<callback>}
\robust@def*\bgdfor{\cpt@teststopt\bgd@for,}
\robust@def\bgd@for[#1]#2:=#3\do#4{%
\advance\bgdfordepth\@ne
\edef\bgdforlist{\ifboolTF{cpt@st}\expandcsonce\unexpanded{#3}}%
\def\getcurrentitem{getcurrentitem@\romannumeral\bgdfordepth}%
\def\getnextitem{getnextitem@\romannumeral\bgdfordepth}%
\bgdbreakforfalse\bgdlastitemfalse
\def\bgdforcount{itemcountonlevel\romannumeral\bgdfordepth}%
\csn@def\bgdforcount{0}%
\csn@def\getcurrentitem##1#1{%
\csn@edef\bgdforcount{\the\numexpr\usename\bgdforcount+1}%
\bgdifx{##1}\bgdfor{%
\bgdbreakforfalse\bgdlastitemfalse
}{%
\edef#2{\unexpanded{##1}}\usename\getnextitem
}%
}%
\long\csn@def\getnextitem##1#1{%
\edef\nextitem{\unexpanded{##1}}%
\bgdifx{##1}\bgdfor{\bgdlastitemtrue\def\nextitem{}}{}%
#4\relax
\ifboolTF{bgdbreakfor}{%
\bgdbreakforfalse\bgdlastitemfalse
\begingroup
\csn@def\getcurrentitem####1\bgdfor#1{\endgroup
\edef\bgdforremainder{\unexpanded{####1}}%
}%
}{}%
\usename\getcurrentitem##1#1%
}%
\bgdifx\bgdforlist\@empty{%
\ifbgdverbose
\@warning{Empty list passed to \noexpand\bgdfor\on@line}%
\fi
}{%
\cpt@sttrue\cpt@csvnormalize[#1]\bgdforlist
\expandafter\usename\expandafter\getcurrentitem\bgdforlist#1\bgdfor#1%
}%
\advance\bgdfordepth\m@one
}
% I saw a source of spurious space in one of the macros of everypage
% package. So I may well define my own hooks. Also, \AtBeginDocument
% seems too early for these hooks.
\AfterStartOfDocument{%
\let\bgd@saved@outputpage\@outputpage
\def\@outputpage{%
\let\bgd@saved@begindvi\@begindvi
\def\@begindvi{%
\bgd@everypage@hook
\bgd@thispage@hook
\let\bgd@thispage@hook\relax
\bgd@saved@begindvi
}%
\bgd@saved@outputpage
\let\@begindvi\bgd@saved@begindvi
}%
}
\new@def*\bgd@everypage@hook{}
\new@def*\bgd@thispage@hook{}
\robust@def*\AtBeginEveryPage{\g@addto@macro\bgd@everypage@hook}
\robust@def*\AtBeginCurrentPage{\g@addto@macro\bgd@thispage@hook}
\robust@def*\AtBeginNextPage{\afterpage}
\ltxkeys@declarekeys[BGD]{background}[bgd]{%
bool/verbose/true;
choice/content.{
from file/.code=\let\bgd@content\bgd@filecontent,
tikzpicture/.code=\let\bgd@content\bgd@tikzcontent,
tikz picture/.code=\let\bgd@content\bgd@tikzcontent,
tikz background/.code=\let\bgd@content\bgd@tikzcontent,
use default/.code=\let\bgd@content\bgd@defaultcontent
}
/from file;
cmd/default content/DRAFT/\def\bgd@defaultcontent{#1};
cmd/position/current page.center/
\edef\bgd@position{\ltxkeys@stripallouterbraces{#1}};
cmd/opacity/1.0/\def\bgd@opacity{#1};
cmd/angle/0/\def\bgd@angle{#1};
cmd/scale/1/\def\bgd@scale{#1};
cmd/color/gray!45/\def\bgd@color{#1};
cmd/xshift/0cm/\def\bgd@hshift{#1};
cmd/yshift/0cm/\def\bgd@vshift{#1};
cmd/anchor//\def\bgd@anchor{#1};
bool/all pages/true/\usename{bgd@allpages#1};
cmd/pages/1/\edef\bgdpages{\ltxkeys@stripallouterbraces{#1}};
bool/odd pages/true/\usename{bgd@oddpages#1};
bool/even pages/true/\usename{bgd@evenpages#1};
bool/no background/true/\usename{bgd@nobackground#1};
cmd/graphics width/2cm/\edef\bgdgraphicswidth{#1};
cmd/graphics height/2cm/\edef\bgdgraphicsheight{#1};
cmd/graphics file/x/
\ifltxkeys@dec\else
\ifblankTF{#1}{%
\@latexerr{Image file is empty}\@ehd
}{%
\edef\bgdgraphicsfile{\ltxkeys@stripallouterbraces{#1}}%
}
\fi;
cmd/viewport/0 5 0 5/
\ifltxkeys@dec\else
\ifblankTF{#1}{\@latexerr{'viewport' is empty}\@ehd}{}
\fi;
cmd/view port/0 5 0 5/\ltxkeys@setaliaskey{viewport};
cmd/every picture//\ifltxkeys@dec\else\everybackground{#1}\fi;
cmd/every background//\ifltxkeys@dec\else\everybackground{#1}\fi;
}
\robust@def*\everybackground#1{\gdef\everybackgroundtoks{#1}}
\everybackground{}
\robust@def*\showeverybackground{%
\typeout{Every picture settings: \bgd@everybackground}%
}
% To-do:
% \addtoeverybackground, \removefromeverybackground, \emptyeverybackground
% This is for imported graphics. Use the key 'content=from file'.
% It is better and faster to always draw your picture for reuse.
\robust@def*\bgd@filecontent{%
\cptexpandsecond\includegraphics{[%
viewport=\bgdviewport,
width=\bgdgraphicswidth,
height=\bgdgraphicsheight,
scale=\bgdscale,% in case you prefer scale.
]{\bgdgraphicsfile}}%
}
% This is for new tikz drawings, to be drawn. For a graphics being
% momentarily drawn with tikZ, complete your design in the environment
% 'tikzbackground' and submit 'tikz picture' or 'tikzpicture'
% as the value of key 'content' in \newbackground.
\robust@def*\tikzbackground{\cpt@testopt\bgd@tikzbackground{}}
\robust@def*\bgd@tikzbackground[#1]{%
\global\setbox\bgd@tempbox=\hbox\bgroup\begin{tikzpicture}[#1]%
}
\robust@def*\endtikzbackground{%
\end{tikzpicture}\egroup
\aftergroup\bgd@tikzb@ckground
}
\robust@def*\bgd@tikzb@ckground{%
\setbox\bgd@mainbox=\vbox{\unvbox\bgd@mainbox\box\bgd@tempbox}%
\def\bgd@tikzcontent{\copy\bgd@mainbox}%
}
\robust@def*\bgd@mastercontent{%
\cptexpandsecond{\begin{tikzpicture}}%
{[remember picture,overlay,\everybackgroundtoks]}
\node [rotate=\bgd@angle,scale=\bgd@scale,opacity=\bgd@opacity,%
xshift=\bgd@hshift,yshift=\bgd@vshift,color=\bgd@color]
at (\bgd@position)[\bgd@anchor]{\bgd@content};
\end{tikzpicture}%
}
\new@def*\background@stack{}
\robust@def*\newbackground{\cpt@testpm\bgd@newbackground}
\robust@def*\bgd@newbackground#1{%
\ifcpt@pm\else
\edef\background@stack{%
\ifx\background@stack\@empty\else\expandcsonce\background@stack;\fi
\unexpanded{#1}%
}%
\fi
}
\new@def*\bgddofifo#1{\bgdfifo#1,\bgdfifo,}
\new@def*\bgdfifo#1,{\bgdifx{#1}\bgdfifo{}{\do{#1}\bgdfifo}}
\robust@def*\bgd@putcontent{%
\bgdfor*[;]\reserveda:=\background@stack\do{%
\def\do####1{\usename{bgd@####1false}}%
\bgddofifo{oddpages,evenpages,allpages}%
\cptexpandargonce{\ltxkeys@setkeys[BGD]{background}}\reserveda
\ifbgd@nobackground\else
\ifbgd@allpages
\bgd@mastercontent
\else
\ifbgd@oddpages
\ifodd\thepage\bgd@mastercontent\fi
\else
\ifbgd@evenpages
\ifodd\thepage\else\bgd@mastercontent\fi
\else
\bgdfor*\reservedb:=\bgdpages\do{%
\ifnum\thepage=\reservedb\relax
\bgd@mastercontent\bgdbreakfor
\fi
}%
\fi
\fi
\fi
\fi
}%
}
\AtBeginEveryPage{\bgd@putcontent}
\robust@def*\nobackgroundonthispage{%
\let\saved@putcontent\bgd@putcontent
\let\bgd@putcontent\relax
\afterpage{%
\let\bgd@putcontent\saved@putcontent
\let\saved@putcontent\relax
}%
}
% To-do:
% Include package options
\endinput
\end{filecontents}
\documentclass[final]{book}
%\usepackage{graphicx}
\usepackage[demo]{graphicx}
\usepackage{lipsum}
\usepackage{showframe}
\usepackage{tikz}
\usepackage{xbackground}
% This is the environment for setting your tikz picture to be used as
% the background. See the note above about the environment 'tikzbackground'.
\begin{tikzbackground}[remember picture,overlay]
\draw [cyan,fill=yellow]
(0cm,0cm)--(2cm,0cm)--(2cm,-2cm)--(0cm,-2cm)--cycle;
\end{tikzbackground}
% User command call with keys. This is where the user gives the background specs.
% The odd details don't concern him/her. You need 2 runs at first use of keys,
% ie, if you change the current value of a key, you may need to compile the
% file twice. Use the 'odd pages' or 'even pages' options to get graphics on
% only odd or even pages. If none of these is given, and 'pages' is also not
% specified, the logo will appear on the first page only. You can decide
% not to insert any graphics at all. You do this by specifying the option
% 'no background'.
% A prime (') on \newbackground means 'ignore this background':
\newbackground'{rubbish}% ignored
% Page 1 gets 2 background pictures:
\newbackground{
xshift=12cm, yshift=-15cm, anchor=below,
position=current page.north west, content=tikz picture,
viewport=20 21 590 400, opacity=1, angle=0.0, scale=.8, % even pages,
graphics file={./Graphics/comet1}, %no background
pages=1
}
\newbackground{
xshift=2.cm, yshift=-2cm, anchor=below,
position=current page.north west, content=from file,
viewport=20 21 590 400, opacity=1, angle=0.0, scale=.8, % even pages,
pages={1,4,5}, graphics file={./Graphics/comet1}, %no background
}
\newbackground{
xshift=1cm, yshift=-2cm, anchor=below,
position=current page.north west, content=tikz picture,
viewport=20 21 590 400, opacity=1, angle=0.0, scale=.8, % even pages,
pages={2,3,6,8}, graphics file={./Graphics/comet1}, %no background
}
\begin{document}
\section*{Lorem Ipsum}
% \nobackgroundonthispage % try this command
\lipsum[1-40]
\end{document}