불완전함 \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.13cm이고 너비가 12.13cm인 매우 큰 문서를 얻게 됩니다.575.84cm키!)

그럼 먼저 교체해보겠습니다독립형~에 의해기사.

사소한 문제(비차단), 좌표 "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' 키를 모르니 무시하겠습니다.소티케이line width=1ptZ는 다음과 같이 본다열쇠, 그렇지 않다열쇠=.

문제는 캐릭터와 관련된 것 같지만 =TeXbook이나 TeX by Topic에서 "보호"에 대한 설명을 찾지 못했지만 아마도 설명을 찾기 위해 좋은 단어를 검색하지 않았을 것입니다.

해결책은 조건부 테스트에서 "="를 출력하는 것입니다.

line width=\ifodd \x {1pt} \else 2pt\fi공장.

중괄호가 없으면 1pt오류가 발생합니다.패키지 PGF 수학 오류: 알 수 없는 함수 `pt'('pt'에서).

메모:괄호를 사용하는 것보다 와 사이를 1pt사용할 수 있습니다 (질문에 대한 설명에서 Skillmon 덕분에).\space\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.

관련 정보