การวนซ้ำชื่อโหนดใน \ 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$};
}
\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
ลูปที่สองในแบบที่ฉันทำ?
คำตอบ
คุณไม่สามารถใช้ตัวเลขร่วมกับตัวอักษรในลำดับการควบคุม
ชื่อลำดับการควบคุมสามารถเป็นได้ทั้งแบบไม่เป็นตัวอักษรเดียวหรือลำดับของตัวอักษรตั้งแต่หนึ่งตัวขึ้นไป
ดังนั้นจึง\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);
}
หมายเลขในเส้นทางหรือชื่อพิกัดเป็นไปตามกฎหมาย แต่ด้วยเหตุผลที่แตกต่างกันโดยสิ้นเชิง
เนื่องจากการแก้ไขโค้ดของคุณเป็นจำนวนมากฉันจึงทำให้โค้ดของคุณง่ายขึ้นโดยใช้ลูปกับตัวแปรเดียวเนื่องจากอีกสองตัวแปรสามารถสร้างขึ้นโดยตัวแปรเดียว

\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}