\ foreach에서 노드 이름을 반복하면 \ inaccessible 오류가 발생합니다.

Aug 20 2020

다음 그림과 같이 원통형 질량을 굵은 수평선으로 연결하고 싶습니다.

다음 코드는 첫 번째 연결에 대해 잘 작동합니다.

\documentclass{standalone}
\usepackage{tikz}
\usetikzlibrary{shapes,intersections}

\begin{document}
\begin{tikzpicture}

\tikzstyle{mass} = [draw, fill=gray!20, cylinder, shape aspect=1, minimum width=1.5cm, minimum height=1cm, shape border rotate=180];

\foreach \xpos/\name/\tag in {0/J1/J_1, 2/J2/J_2, 4/J3/J_3, 6/J4/\cdots, 8/J5/J_n}
    {
    \node[mass, name=\name] at (\xpos cm,0cm) {};
    \draw[shift=(\name.center)] node[] {$\tag$};
    }

\path[name path=line1] (J2.before top) -- (J2.after top);
\path[name path=line2] (J2.top) -- (J2.bottom);
\draw[name intersections={of=line1 and line2}, thick] (J1.east) -- (intersection-1);

\end{tikzpicture}

명백한 이유로 \foreach루프를 사용하여 연결하고 싶습니다 . 다음을 시도했습니다.

\documentclass{standalone}
\usepackage{tikz}
\usetikzlibrary{shapes,intersections}

\begin{document}
\begin{tikzpicture}

\tikzstyle{mass} = [draw, fill=gray!20, cylinder, shape aspect=1, minimum width=1.5cm, minimum height=1cm, shape border rotate=180];

\foreach \xpos/\name/\tag in {0/J1/J_1, 2/J2/J_2, 4/J3/J_3, 6/J4/\cdots, 8/J5/J_n}
    {
    \node[mass, name=\name] at (\xpos cm,0cm) {};
    \draw[shift=(\name.center)] node[] {$\tag$};
    }

\foreach \name1/\name2 in {J1/J2, J2/J3, J3/J4, J4/J5}
    {
    \path[name path=line1] (\name2.before top) -- (\name2.after top);
    \path[name path=line2] (\name2.top) -- (\name2.bottom);
    \draw[name intersections={of=line1 and line2}, thick] (\name1.east) -- (intersection-1); 
    }

\end{tikzpicture}

불행히도 두 번째 예제에서는 두 번째 \foreach루프가 작동하지 않습니다. LaTeX는 \inaccessible컴파일 중에 오류를 발생시킵니다. 두 번째 \foreach루프에서 내가 한 방식으로 노드 이름에 액세스 할 수없는 것 같습니다 .

답변

4 egreg Aug 20 2020 at 03:46

제어 시퀀스에서 문자와 함께 숫자를 사용할 수 없습니다.

제어 순서 이름은 문자가 아닌 단일 문자이거나 하나 이상의 문자 순서 일 수 있습니다.

따라서이 \name1문맥에서 그리고 일반적으로 불법적 인 이름입니다. 어떤 상황에서는 작동하는 것처럼 보일 수 있지만 이것에 의존하지 마십시오.

사용하다

\foreach \namea/\nameb in {J1/J2, J2/J3, J3/J4, J4/J5}
    {
    \path[name path=line1] (\nameb.before top) -- (\nameb.after top);
    \path[name path=line2] (\nameb.top) -- (\nameb.bottom);
    \draw[name intersections={of=line1 and line2}, thick] (\namea.east) -- (intersection-1); 
    }

경로 또는 좌표 이름의 숫자는 합법적이지만 완전히 다른 이유가 있습니다.

4 AndréC Aug 20 2020 at 03:43

egreg가 코드를 수정 했으므로 다른 두 개는 단일 변수에 의해 생성 될 수 있으므로 단일 변수가있는 루프를 사용하여 코드를 단순화합니다.

\documentclass[border=5mm,tikz]{standalone}
\usetikzlibrary{shapes,intersections}

\begin{document}

\begin{tikzpicture}

\tikzset{mass/.style={draw, fill=gray!20, cylinder, shape aspect=1, minimum width=1.5cm, minimum height=1cm, shape border rotate=180}}

\foreach \tag[count=\xpos from 0] in {J_1,J_2,J_3,\cdots,J_n}
    {
    \node[mass, name=J\xpos] at (2*\xpos,0) {};
    \draw[shift=(J\xpos.center)] node[] {$\tag$};
    }

\foreach \n [evaluate=\n as \lastn using int(\n+1)] in {0,1,2,3}
    {
    \path[name path=line1] (J\lastn.before top) -- (J\lastn.after top);
    \path[name path=line2] (J\lastn.top) -- (J\lastn.bottom);
    \draw[name intersections={of=line1 and line2}, thick] (J\n.east) -- (intersection-1); 
    }

\end{tikzpicture}
\end{document}