Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

How can i place an image on the top of the specified page. For example odd pages will be filled by image, even with text. Or, i just want to make sure that (lua)latex will place images on page 2,3,6?

Similar problem was discussed here: How to include a picture over two pages, left part on left side, right on right (for books)?

share|improve this question

2 Answers 2

Here is a version of a solution to How do I add an image in the upper, left-hand corner using TikZ and graphicx adapted to only add the specified logo on pages 2,3, and 6. It uses the [some] option to the background package.

To change the pages on which the graphic is placed, you simply need to change the values in the definition of \ListOfPagesWithLogo. With that macro set as:

\newcommand*{\ListOfPagesWithLogo}{2,3,6}

one obtains the logo on pages 2, 3, and 6:

enter image description here

Notes:

  • I used the xstring package for numerical comparisons as I like that syntax but this can be adapted to not require that.
  • This version below places a tikz picture, but you can un-commented the \SetBgContents line if you wish to include an external graphic.
  • Two compile runs are required, the first to obtain the coordinates, the second to do the actual drawing.

Code:

% Modified version of http://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}
share|improve this answer
    
The author/maintainer of background package should look at the command \NoBgThispage again. As it is now, it can be called only once. The same applies to \SetBgContents and associated commands. –  Ahmed Musa Jun 14 '12 at 4:33
    
@GonzaloMedina: You want to AhmedMusa's comment? –  Peter Grill Jun 14 '12 at 4:39

Here is a package based on keys. There is no dependence on background and everypage packages.

\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}

enter image description here

share|improve this answer
    
Works great. I would recommend you add the [demo] option to the graphicx package so that others without the specific image file can run the MWE via cut and paste. –  Peter Grill Jun 12 '12 at 0:17
    
[demo] added. –  Ahmed Musa Jun 12 '12 at 5:18

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.