newif 指令的工作原理

newif 指令的工作原理

因為我在這裡沒有得到我的問題的答案:

如何使用 pgfgantt 套件繪製垂直線,我決定實現我自己添加到pgfgantt.sty文件中的命令(我已經製作了副本)。我查看了一下pgfgantt.sty,想出了這段程式碼,它只能解決一個問題:

\newcommand\drawverticalline[1]{%
\begingroup%
\begin{pgfinterruptboundingbox}%
\begin{scope}
 \newif\ifgtt@includetitle
   \ganttset{%
      include title in canvas/.is if=gtt@includetitle,%
      include title in canvas
      }
   \gtt@tsstojulian{#1}{\gtt@today@slot}
   \gtt@juliantotimeslot{\gtt@today@slot}{\gtt@today@slot}%
   \ifgtt@includetitle%
    \def\y@upper{0}%
   \else%
     \pgfmathsetmacro\y@upper{%
      \gtt@lasttitleline * \ganttvalueof{y unit title}%
     }%
   \fi%
  \pgfmathsetmacro\y@lower{%
  \gtt@lasttitleline * \ganttvalueof{y unit title}%
  + (\gtt@currentline - \gtt@lasttitleline - 1)%
  * \ganttvalueof{y unit chart}%
  }%
  \pgfmathsetmacro\x@mid{%
     (\gtt@today@slot - 1 + \ganttvalueof{today offset})%
                    * \ganttvalueof{x unit}%
   }%
  \draw [/pgfgantt/today rule]
                (\x@mid pt, \y@upper pt) -- (\x@mid pt, \y@lower pt)
                node [/pgfgantt/today label node] {\ganttvalueof{today   label}};%
 \end{scope}
 \end{pgfinterruptboundingbox}%
  \endgroup%
}

這只是一個例子來證明它是有效的。這是我透過這段程式碼得到的結果:

\documentclass{article}
\usepackage[frenchb]{babel}  
\usepackage{pgfgantt}
\usetikzlibrary{shadows}

\begin{document}
\begin{tikzpicture} % optional
   \begin{ganttchart}[x unit=1.8mm, 
                  y unit chart=0.87cm, 
                  time slot format=isodate, 
                  vgrid=*{5}{dotted},
                 ]
                  {2014-04-14}{2014-07-11}
       \gantttitlecalendar{month=name} \\ 

       \ganttbar[progress=100]{title1}{2014-04-14}{2014-04-15} \\

       \ganttbar[progress=100]{title2}{2014-04-15}{2014-04-17} \\

       \drawverticalline{2014-05-07}
   \end{ganttchart}
\end{tikzpicture}
\end{document}

在此輸入影像描述

通常該線應繪製在標題列下方。這段程式碼定義了我們開始繪製的位置:

\ifgtt@includetitle%
  \def\y@upper{0}%
\else%
  \pgfmathsetmacro\y@upper{%
    \gtt@lasttitleline * \ganttvalueof{y unit title}%
  }%
\fi%

如何\ifgtt@includetitle運作?它的定義如下:

\newif\ifgtt@includetitle
\ganttset{%
    include title in canvas/.is if=gtt@includetitle,%
    include title in canvas
    }

答案1

\newif(在 LaTeX 中)由以下定義。 (原版的定義略有不同)。

\def\newif#1{%
  \count@\escapechar \escapechar\m@ne
    \let#1\iffalse
    \@if#1\iftrue
    \@if#1\iffalse
  \escapechar\count@}
\def\@if#1#2{%
  \expandafter\def\csname\expandafter\@gobbletwo\string#1%
                    \expandafter\@gobbletwo\string#2\endcsname
                       {\let#1#2}}

因此 \newif採用命令名稱(按照慣例總是以 開頭if)並定義三個命令

\newif\iffoo定義

\iffoo 成為\iffalse 並定義

\footrueto be 一個指令,定義\iffoo\iftrue並定義

\foofalse是一個定義\iffoo為 的命令\iffalse

\iftrue\iffalse是分別充當 true 和 false 的原語。

所以如果你有

\iffoo

  some code here

\fi

程式碼是否會被執行取決於您之前是否執行過更改to\footrue的含義。\iffoo\iftrue

相關內容