Page LayoutA problem concerning use of xkeyval package

Information and discussion about page layout specific issues (e.g. header and footer lines, page formats, page numbers).
Post Reply
Polestar
Posts: 5
Joined: Tue Oct 07, 2008 2:43 pm

A problem concerning use of xkeyval package

Post by Polestar »

I am attempting to extend the 'draftwatermark' by
Sergio Callegari to meet a special need. I call the
new package 'draftmark'. I will like to use a macro
I call \draftmarksetup to specify package options,
as an alternative to specifying the optional arguments
to the \usepackage command. I have used the facilities
in the xkeyval package for processing my package
options, but the macro \draftmarksetup based on \setkeys
has not worked properly. Can anyone familiar with the
xkeyval/keyval package help me?
The new package and a sample document are listed below.

Code: Select all

\ProvidesPackage{draftmark}[2009/03/10 v1.0 Puts a draftmark on specified document pages]
\NeedsTeXFormat{LaTeX2e}
\RequirePackage{etoolbox}
\RequirePackage{xkeyval}
\RequirePackage{atbegshi}
\RequirePackage{picture}
\RequirePackage{xcolor}
\RequirePackage{xifthen}
\newif\ifdm@final\dm@finalfalse
\newif\ifdm@draft\dm@draftfalse
\newif\ifdm@firstpage\dm@firstpagefalse
\newif\ifdm@allpages\dm@allpagesfalse
\newif\ifdm@oddpages\dm@oddpagesfalse
\newif\ifdm@evenpages\dm@evenpagesfalse
\newif\ifdm@particularpage\dm@particularpagefalse
\newlength\dm@xcoord
\setlength\dm@xcoord{0.5\paperwidth}
\newlength\dm@ycoord
\setlength\dm@ycoord{-0.5\paperheight}
\newcount\dm@sytlecnt\dm@sytlecnt=\z@
\newlength\dm@xcoord@tempa
\newlength\dm@ycoord@tempa
% Defaults:
\def\dm@markangle{45}
\def\dm@markscale{1}
\edef\dm@draftmarkpages{\z@}
\edef\dm@markcolorname{gray}
\edef\dm@markgrayness{0.8}
\def\dm@markcolor{\color[gray]{0.8}}
\edef\dm@marktext{DRAFT}
\edef\dm@fontfamily{cmr}
\edef\dm@fontsize{5cm}
\newcommand\dm@info[1]{\PackageInfo{draftmark}{#1\@gobble}}
\newcommand\dm@warn[1]{\PackageWarningNoLine{draftmark}{#1}}
\@onlypreamble\dm@warn
\@onlypreamble\dm@info
\DeclareOptionX[dm]<draftmark>{firstpage}[true]{%
  \ifstrequal{#1}{true}{\dm@firstpagetrue}
    {\dm@firstpagefalse}
  \xdef\dm@draftmarkpages{First}
  \dm@sytlecnt=\@ne}
\DeclareOptionX[dm]<draftmark>{allpages}[true]{%
  \ifstrequal{#1}{true}{\dm@allpagestrue}
    {\dm@allpagesfalse}
  \xdef\dm@draftmarkpages{all}
  \dm@sytlecnt=\@ne}
\DeclareOptionX[dm]<draftmark>{oddpages}[true]{%
  \ifstrequal{#1}{true}{\dm@oddpagestrue}
    {\dm@oddpagesfalse}
  \xdef\dm@draftmarkpages{odd}
  \dm@sytlecnt=\@ne}
\DeclareOptionX[dm]<draftmark>{evenpages}[true]{%
  \ifstrequal{#1}{true}{\dm@evenpagestrue}
    {\dm@evenpagesfalse}
  \xdef\dm@draftmarkpages{even}
  \dm@sytlecnt=\@ne}
\DeclareOptionX[dm]<draftmark>{pageno}[1]{%
  \dm@particularpagetrue
  \edef\dm@pageno{#1}
  \xdef\dm@draftmarkpages{#1}
  \dm@sytlecnt=1}
\DeclareOptionX[dm]<draftmark>{draft}[true]{%
  \ifstrequal{#1}{true}{\dm@drafttrue}{\dm@draftfalse}
  \global\dm@finalfalse}
\DeclareOptionX[dm]<draftmark>{final}[true]{%
  \ifstrequal{#1}{true}{\dm@finaltrue}{\dm@finalfalse}
  \global\dm@draftfalse}
\DeclareOptionX[dm]<draftmark>{fontfamily}[cmr]{%
  \edef\dm@fontfamily{#1}}
\DeclareOptionX[dm]<draftmark>{mark}[DRAFT]%
  {\edef\dm@marktext{#1}}
\DeclareOptionX[dm]<draftmark>{color}[gray]%
  {\edef\dm@markcolorname{#1}}
\DeclareOptionX[dm]<draftmark>{grayness}[0.8]%
  {\edef\dm@markgrayness{#1}}
\DeclareOptionX[dm]<draftmark>{angle}[45]%
  {\edef\dm@markangle{#1}}
\DeclareOptionX[dm]<draftmark>{scale}[1]%
  {\edef\dm@markscale{#1}}
\DeclareOptionX[dm]<draftmark>{xcoord}[0]%
  {\setlength\dm@xcoord@tempa{#1mm}}
\DeclareOptionX[dm]<draftmark>{ycoord}[0]%
  {\setlength\dm@ycoord@tempa{#1mm}}
\DeclareOptionX*{\dm@warn{Unknown option
  '\CurrentOption' ignored}}
\ExecuteOptionsX[dm]<draftmark>{final}
\ProcessOptionsX[dm]<draftmark>\relax
% >>> Definition of \draftmarksetup that doesn't
%     work properly when called.
\newcommand\draftmarksetup[1]{\setkeys[dm]{draftmark}{#1}}
\@onlypreamble\draftmarksetup
\dm@info{mark=\dm@marktext,\space printed on page
  '\dm@draftmarkpages',^^J
  color=\dm@markcolorname,\space angle=\dm@markangle,
  \space scale=\dm@markscale}
\ifdefstring{\dm@markcolorname}{gray}{
  \def\dm@markcolor{\color[gray]{\dm@markgrayness}}}
  {\def\dm@markcolor{\color{\dm@markcolorname}}}
\newcommand\dm@draftmark{
  \makebox(0,0){\rotatebox{\dm@markangle}{
  \scalebox{\dm@markscale}{\dm@markcolor{
  \fontfamily{\dm@fontfamily}\fontseries{m}
  \fontsize{\dm@fontsize}{1.2\dm@fontsize}%
  \selectfont
  \dm@marktext}\normalcolor}}}}
\addtolength\dm@xcoord{\dm@xcoord@tempa}
\addtolength\dm@ycoord{\dm@ycoord@tempa}
\newcommand\dm@printdraftmark@tempa{
  \put(\dm@xcoord,\dm@ycoord){\dm@draftmark}
}
\newcommand\dm@printdraftmark{
  \AtBeginShipout{\AtBeginShipoutUpperLeft{
  \begingroup
  \ifdm@allpages
    \dm@printdraftmark@tempa
  \else\ifdm@firstpage
    \ifnum\arabic{page}=\@ne\dm@printdraftmark@tempa\fi
  \else\ifdm@oddpages
    \ifodd\arabic{page}\dm@printdraftmark@tempa\fi
  \else\ifdm@evenpages
    \ifodd\arabic{page}\else\dm@printdraftmark@tempa\fi
  \else\ifdm@particularpage
    \ifnum\arabic{page}=\dm@pageno\dm@printdraftmark@tempa\fi
  \fi\fi\fi\fi\fi
  \endgroup
}}}
\ifdm@final
  \dm@info{Option 'final': no draftmark printed}
\else\ifdm@draft
  \dm@printdraftmark
  \dm@info{Option 'draft': draftmark printed}
  \ifnum\the\dm@sytlecnt=\z@
    \dm@warn{Draftmark printed on 0 pages:\MessageBreak
    No page option ('allpages', 'firstpage',\MessageBreak
    'oddpages', 'evenpages' or 'pageno=value')\MessageBreak
    passed to package}
\fi\fi\fi
\endinput

%%% Sample document %%%
\documentclass[a4paper,10pt]{article}
\usepackage{xcolor}
\usepackage{comment}
\usepackage{show2e}
\begin{comment}
  The following worked:
  \usepackage[draft=true,fontfamily=ptm,allpages=true,
  angle=45,xcoord=0,ycoord=0,scale=0.8,mark=Confidential,
  color=red!75!green!50,grayness=0.6]{draftmark}
\end{comment}
% >> But the following failed to pick some of the options:
  \usepackage{draftmark}
  \draftmarksetup{draft=true,fontfamily=ptm,allpages=true,
  angle=45,xcoord=0,ycoord=0,scale=0.8,mark=Confidential,
  color=red!75!green!50,grayness=0.6}

\begin{comment}
  Has the macro \dm@draftmark@draft received the value 'true'?
  \makeatletter
  \showcmd\dm@draftmark@draft
  \makeatother
\end{comment}

\begin{document}
\section{Test section}
\subsection{Test subsection}
\newpage
\part{Test part}
\section{Test section}
\newpage
\part{Another test part}
\section{Test section}
\end{document}

Recommended reading 2024:

LaTeXguide.org • LaTeX-Cookbook.net • TikZ.org

NEW: TikZ book now 40% off at Amazon.com for a short time.

Polestar
Posts: 5
Joined: Tue Oct 07, 2008 2:43 pm

A problem concerning use of xkeyval package

Post by Polestar »

The right syntax appears to be:

Code: Select all

\usepackage[draft=true]{draftmark}
\draftmarksetup{%
  allpages=true,
  angle=45,
  color=gray,
  fontfamily=phv,
  grayness=0.9,
  mark=Confidential,
  scale=.8,
  xcoord=0,
  ycoord=0
}
% \lipsum[1-4]
Either draft=true or final=true must be set at the point of issuing \usepackage. Other options can then be set dynamically afterwards by \draftmarksetup. This worked in all the cases I have tried.
Post Reply