不完全な\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 cm の非常に大きなドキュメントが作成されます)。575.84センチメートル身長!)

まず、スタンドアロンによる記事

軽微な問題(非ブロッキング)ですが、座標「aux3」と「aux4」は使用されていないため、消去します。

2 番目の問題は、行の構文にあります\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' は分からないので無視します。だからティline width=1ptZは次のように見る、としてではなく=価値

この問題は文字に関連しているようです=が、TeXbook でもトピック別の TeX でも「protect」の説明が見つかりませんでした。おそらく、説明を見つけるのに適切な単語を検索しなかったのでしょう。

解決策としては、条件テストから「=」を出力することです。

line width=\ifodd \x {1pt} \else 2pt\fi動作します。

括弧なしでは1ptエラーが発生しますパッケージ PGF 数学エラー: 不明な関数 `pt' ('pt' 内)。

注記:を括弧で囲むよりも1pt、と\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

関連情報