
내가 하고 있는 일:
나는 선의 노드에 대한 일부 간격의 수축을 설명하기 위해 그림(아래 참조)을 그리고 싶습니다.
newcommand
먼저 시작점, 끝점 및 이름의 세 가지 인수를 사용하여 for 간격(예: \op)을 정의합니다 . 그런 다음 선(또는 체인)이 그려집니다. 마지막으로 간격과 해당 라인의 해당 노드가 연결됩니다. 전체 코드는 다음과 같습니다: (메모:다음에서 공유 코드를 읽고 수정할 수도 있습니다.공유라텍스답변이 수락되기 전에.)
\documentclass{article}
\usepackage[utf8]{inputenc}
\usepackage{tikz}
\usetikzlibrary{calc, chains}
\title{Reference Arguments in Newcommand}
\author{hengxin}
\date{31 December 2013}
\begin{document}
\begin{tikzpicture}[]
% new command: inverval
\newcommand{\op}[3] % #1: start point; #2: end point; #3: interval name
{
\coordinate (start) at #1; % start point
\coordinate (end) at #2; % end point
\coordinate (mid) at ($0.5*#1 + 0.5*#2 + (0,0.8cm)$);
\draw[ultra thick, blue, |-|] (start) -- (end); % draw the interval
\node (#3) [font = \huge] at (mid) {#3}; % attach the operation name
}
% define three intervals
\op{(0,2)}{(2,2)}{$a$};
\op{(4,3)}{(6,3)}{$b$};
\op{(7,2)}{(10,2)}{$c$};
% contract them into single nodes in a line (or a chain)
\begin{scope}[font = \huge, start chain = schedule, node distance = 3.0cm, every join/.style = {very thick, ->, red}]
\foreach \opi in {a, b, c}
\node (\opi) [circle, draw, on chain, join, label = {[] below : $\opi$}] {};
\end{scope}
% connect interval and its corresponding node in the line
\begin{scope}[cedge/.style = {->, dashed, draw, thick}]
\draw [cedge] (0,2) to (a);
\draw [cedge] (2,2) to (a);
\draw [cedge] (4,3) to (b);
\draw [cedge] (6,3) to (b);
\draw [cedge] (7,2) to (c);
\draw [cedge] (10,2) to (c);
\end{scope}
\end{tikzpicture}
\end{document}
내가 하고 싶은 것:
보시다시피, 마지막 단계(간격과 해당 라인의 해당 노드 연결)에서 와 같이 각 간격의 시작점과 끝점의 좌표를 하드 코딩했습니다 \draw [cedge] (0,2) to (a); \draw [cedge] (2,2) to (a);
. 확실히 지루하고 오류가 발생하기 쉽습니다. 그러므로,
newcommand
하드 코드 없이 각 간격의 두 지점(의 인수로)을 참조할 수 있는 우아한 방법이 있습니까 ?
답변1
명령 에서 \op
간격의 각 제한 이름을 지정할 수 있습니다( #3
이름의 일부로 사용).
\documentclass{standalone}
\usepackage{tikz}
\usetikzlibrary{calc, chains}
\begin{document}
\begin{tikzpicture}
% new command: inverval
\newcommand{\op}[3]{ % #1: start point; #2: end point; #3: operation name
\coordinate (start #3) at #1; % start point
\coordinate (end #3) at #2; % end point
\draw[ultra thick, blue, |-|] (start #3) -- (end #3) % draw the interval
node[pos=.5,above=8mm,font=\huge, text=black] {$#3$}; % attach the operation name
}
% define three intervals
\op{(0,2)}{(2,2)}{a};
\op{(4,3)}{(6,3)}{b};
\op{(7,2)}{(10,2)}{c};
% contract them into single nodes in a line (or a chain)
\begin{scope}[font=\huge, start chain=schedule, node distance=3.0cm,
every join/.style={very thick, ->, red}]
\foreach \opi in {a, b, c} {
\node [circle,draw,on chain,join,label={[]below:$\opi$}] (\opi) {};
}
\end{scope}
% connect interval and its corresponding node in the line
\begin{scope}[cedge/.style = {->, dashed, draw, thick}]
\foreach \interval in {a,b,c}{
\draw [cedge] (start \interval) to (\interval);
\draw [cedge] (end \interval) to (\interval);
}
\end{scope}
\end{tikzpicture}
\end{document}