10

Note: I don't think the ifdraft package provides exactly what I'm looking for, but it could be wrappered (I just don't know how :-) )

I'd like to do this:

\ifdraft
... pgf graph component that's only for me
\fi

I'm currently using \iffalse and \fi, but it's obviously more clunky to change 20 locations instead of one. I'm trying to avoid \ifdraft{do x}{do y} because 1) it's uglier and therefore less readable 2) 95% of my use cases would be \ifdraft{do X}{}. If there's a way to wrapper ifdraft{do X}{} into \ifdraft X \fi that would be perfect!

Hamy
  • 423

3 Answers3

11

You could change the meaning of \ifdraft:

\documentclass[draft]{article}
\usepackage{ifdraft}

% save a copy of \ifdfraft
\let\ORIifdraft\ifdraft
% redefine it to be a normal conditional with \else and \fi
% according to the document condition
\ORIifdraft{\let\ifdraft\iftrue}{\let\ifdraft\iffalse}

\begin{document}

\ifdraft
 DRAFT
\fi

Text.

\end{document}

Or, better, you can define your own new conditional. This is my recommended way.

\documentclass[draft]{article}
\usepackage{ifdraft}

% define a new conditional with \else and \fi
% according to the document condition
\ifdraft{\let\ifxdraft\iftrue}{\let\ifxdraft\iffalse}

\begin{document}

\ifxdraft
 DRAFT
\fi

Text.

\end{document}
egreg
  • 1,121,712
10

Use a \newif.

\documentclass{article}
\newif\ifdraft
\drafttrue % or \draftfalse
\begin{document}
\ifdraft
This is a draft.
\else
This isn't.
\fi
\end{document}

The \else part can be omitted.

Ian Thompson
  • 43,767
  • I have a similar issue. I want to display the Git hash info - from the VC package - when in draft mode and then hide it when switching to final. In my Makefile, I am simply doing the following:
    `.tex.pdf:`
    `pdflatex $<`
    
    

    Followed by passing in the filename. Could I use something like make $file-draft and make $file-final to accomplish this?

    – Chris Aug 23 '19 at 14:31
  • @Chris --- I think it would be best to ask a new question about this. – Ian Thompson Aug 30 '19 at 15:14
5

You can define your own \ifdraftT that can be used as \ifdraftT{do X} if you will.

Otherwise, a true TeX if \ifdraft needs to be \let to \iffalse or \iftrue or things could go wrong …

With the ifdraft package loaded, you can simply

\let\ifdraft\if@draft

and \ifdraft works as the internal \if@draft (which is used by the original \ifdraft).

Code

\documentclass[draft]{article}
\usepackage{ifdraft}
\makeatletter
\let\ifdraft\if@draft
\newcommand*{\ifdraftT}{\if@draft\expandafter\@firstofone\else\expandafter\@gobble\fi}
\makeatother
\begin{document}
Foo\par
\ifdraft
  something
\fi

Bar\par
\ifdraftT{something else}

Biba
\end{document}
Qrrbrbirlbel
  • 119,821