不完整\ifodd;行後所有文字都被忽略

不完整\ifodd;行後所有文字都被忽略

我在使用時遇到很多麻煩\ifodd。我想製作一個粗細線交替的圖案,我嘗試了幾種方法,但都不起作用。

有了這個結構


\documentclass{standalone}
\usepackage{tikz}
\usetikzlibrary{positioning,calc}

\begin{document}
\begin{tikzpicture}[remember picture,overlay,shorten >= -10pt]

    \coordinate (aux1) at ([xshift=-25mm, yshift=-30mm]current page.north east);
    \coordinate (aux2) at ([xshift=-25mm, yshift=30mm]current page.south east);
    \coordinate (aux3) at ([xshift=25mm, yshift=-30mm]current page.north west);
    \coordinate (aux4) at ([xshift=25mm, yshift=30mm]current page.south west);
    
    \begin{scope}[black]
        \foreach \i [evaluate =\i as \x using int(\i)] in {0, ..., 4}{
            \draw["\ifodd \x line width=1pt \else line width=2pt \fi" ]([
                    xshift=\x mm,
                    yshift=-65mm
                ]aux1)
                -- 
                ([
                    xshift=\x mm,
                    yshift=10mm]
                aux2);
        }
        
    \end{scope}
\end{tikzpicture}

\end{document}

我努力了


\foreach \x in {0, ..., 4}{
    \draw["\ifodd \x line width=1pt \else line width=2pt \fi" ]([
}
\foreach \x in {0, ..., 4}{
    \draw["\ifodd \num{\x} line width=1pt \else line width=2pt \fi" ]([
}
\foreach \x in {0, ..., 4}{
    \draw["\ifodd \value{\x} line width=1pt \else line width=2pt \fi" ]([
}
\foreach \i [evaluate =\i as \x using int(\i)] in {0, ..., 4}{
    \draw["\ifodd \x line width=1pt \else line width=2pt \fi" ]([
}

注意:我想學習如何在循環內使用 \ifodd

答案1

您的程式碼存在幾個問題。第一個是使用「獨立」文檔類,其節點相對於目前頁面的一角定位(但對於獨立,頁面的尺寸不固定)。這會導致“尺寸太大”錯誤(註釋以 開頭的行\draw,您將獲得一個非常大的文檔:寬度為 12.13 厘米,575.84 厘米高度!

所以首先,我們替換獨立的經過文章

小問題(非阻塞),座標“aux3”和“aux4”沒有被使用,所以我們擦除它。

第二個問題是行中的語法\draw["\ifodd \x line width=1pt \else line width=2pt \fi" ]([

首先,忘記引號",這是錯誤的語法。

其次,如果你刪除後嘗試"\ifodd \x line width=1pt \else line width=2pt \fi仍然不起作用。

\ifodd \x red\else blue\fi作品。

事實上,如果你在周圍添加大括號真實程式碼假代碼,就像\ifodd \x{line width=1pt}\else{line width=2pt}\fi您收到錯誤訊息一樣我不知道關鍵“/tikz/line width=2pt”,我將忽略它。硫鈦kZline width=1pt視為鑰匙,不作為鑰匙=價值

這個問題似乎與角色有關=,但我在 TeXbook 和 TeX by Topic 中都沒有找到「保護」它們的解釋,但我可能沒有搜尋好詞來找到解釋。

解決方案是從條件測試中輸出“=”。

line width=\ifodd \x {1pt} \else 2pt\fi作品。

如果沒有大括號1pt,就會出現錯誤包 PGF 數學錯誤:未知函數“pt”(在“pt”中)。

筆記:比在周圍使用大括號更好1pt,我們可以在and\space之間使用(感謝 Skillmon,在問題的評論中)。\ifodd \x1pt

編輯:您也可以直接用於\i測試和水平移位的尺寸(因此您可以在命令中抑制[evaluate =\i as \x using int(\i)]並替換xshift=\x mm,為)。xshift=\i mm,\draw

更正後的程式碼(我還修改了yshift繪圖指令中的尺寸,因此線條垂直居中):

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{positioning,calc}

\begin{document}
\begin{tikzpicture}[remember picture,overlay]

    \coordinate (aux1) at ([xshift=-25mm, yshift=-30mm]current page.north east);
    \coordinate (aux2) at ([xshift=-25mm, yshift=30mm]current page.south east);
    
    \begin{scope}[black]
        \foreach \i in {0, ..., 4}{
            \draw[line width=\ifodd \i\space 1pt\else 2pt\fi]([
                    xshift=\i mm,
                    yshift=0mm
                ]aux1)
                -- 
                ([
                    xshift=\i mm,
                    yshift=0mm]
                aux2);
        }
        
    \end{scope}
\end{tikzpicture}

\end{document}

輸出(左:整個文檔,右:放大):

在此輸入影像描述

答案2

選項中不能包含條件。

我不確定你為什麼\x這樣定義,因為\i 一個整數。

比這更簡單:使用\pgfmathifthenelse.

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{positioning,calc}

\begin{document}
\begin{tikzpicture}[
%  remember picture,
%  overlay,
  shorten >= -10pt
]
  \coordinate (aux1) at ([xshift=-25mm, yshift=-30mm]current page.north east);
  \coordinate (aux2) at ([xshift=-25mm, yshift=30mm]current page.south east);
  \coordinate (aux3) at ([xshift=25mm, yshift=-30mm]current page.north west);
  \coordinate (aux4) at ([xshift=25mm, yshift=30mm]current page.south west);
    
  \begin{scope}[black]
    \foreach \i in {0, ..., 4}{
      \pgfmathifthenelse{isodd(\i)}{1}{2}
      \draw[line width=\pgfmathresult pt]
        ([xshift=\i mm,yshift=-65mm]aux1)
        -- 
        ([xshift=\i mm,yshift=10mm]aux2);
    }
  \end{scope}
\end{tikzpicture}

\end{document}

在此輸入影像描述

正如 Qrrbrbirlbel 在評論中指出的,你可以更簡單地做

      \draw[line width=isodd(\i) ? 1pt : 2pt]

省略該\pgfmathifhenelse行。

或者你可以使用expl3

\ExplSyntaxOn
\NewExpandableDocumentCommand{\isoddTF}{mmm}
 {
  \int_if_odd:nTF { #1 } { #2 } { #3 }
 }
\ExplSyntaxOff

在序言中和

     \draw[line width=\isoddTF{\i}{1pt}{2pt}]

的體內tikzpicture

相關內容