TeX.SX 的各位大家好。我被以下任务困扰;我需要创建某种类似于附图的树形图。我正在提供 MWE。
\documentclass[letterpaper]{article}
\usepackage[
text={6.5in,9in},
centering,
papersize={8.5in,11in},
top=1in,
heightrounded,
bindingoffset=0.2in%,
%showframe
]{geometry}
\usepackage{tikz}
\usepackage{enumitem}
\begin{document}
\noindent
\begin{center}
\begin{tikzpicture}[every node/.style = {shape = rectangle,
draw,
minimum width = 3cm,
minimum height = 1.2cm,
inner sep = 3pt,
align = center,
font =\footnotesize,
text = black}]
% the nodes : possible \newcommand*\dx{5} \newcommand*\dy{2}
\node[draw=white](0;-1) at (0,1.5) {\textbf{For Patients Who Opt Out}};
\node(0;0) at (0,0) {%
\parbox{0.3\linewidth}{%
\begin{itemize}[leftmargin=1.5em]
\item Counsel on testing benefits
\item Identify barriers to testing
\item Provide emotional support
\item Re-assess intention to test
\end{itemize}
}};
\node[draw=white](0;1) at (0,-2.5) {\textbf{Specimen Collected}};
\node(0;2) at (0,-3.5) {%
\parbox{0.3\linewidth}{%
Specimen for HIV test collected and if rapid HIV test used (10-20 minute wait for results) as per national algorithm.
}};
\node[draw=white](0;3) at (0,-5) {\textbf{Further testing performed if indicated}};
\node(0;4) at (0,-6) {%
\parbox{0.3\linewidth}{%
Further testing for positives and inclusive as per national algorithm.
}};
\node(1;1) at (6, 4) {%
\parbox{0.3\linewidth}{%
\textbf{Provider Initiated Counseling Testing}\\ – Routine Offer Average time= 20-30 minutes}};
\node(1;2) at (6, 1) {%
\parbox{0.3\linewidth}{%
\begin{itemize}[leftmargin=1.5em]
\item Group Education/HIV Information
\item HIV test recommended
\item Confidentiality assured
\item Consent obtained using ``opt-out'' language
\end{itemize}
}};
\node[draw=white](1;3) at (6, -1.5) {\textbf{Brief Risk Reduction}};
\node(1;4) at (6,-2.5) {%
\parbox{0.3\linewidth}{%
Brief Risk-Reduction Counseling may be performed while awaiting rapid test result.
}};
\node[draw=white](1;5) at (6,-4.25) {\textbf{Provider Gives Test Results}};
\node(1;6) at (6,-5.25) {%
\parbox{0.3\linewidth}{%
Provider delivers HIV test results and interprets meaning.
}};
\node[draw=white](1;7) at (6,-7) {\textbf{Post-Test Advice and Referral}};
\node(1;8) at (6,-8) {%
\parbox{0.3\linewidth}{%
Provider offers information about referrals, and if positive, refers to treatment and care and advises on the importance of disclosure.
}};
\node(2;1) at (-6, 4) {%
\parbox{0.3\linewidth}{%
\textbf{Voluntary Counseling and Testing}\\ Average time = 1hr.}};
\node(2;2) at (-6, 1.35) {%
\parbox{0.3\linewidth}{%
\begin{itemize}[leftmargin=1.5em]
\item Client request test
\item Explanation of HIV/AIDS
\item Review prevention measures
\item Consent requested
\end{itemize}
}};
\node[draw=white](2;3) at (-6, -1) {%
\parbox{0.3\linewidth}{%
\textbf{HIV Risk reduction Counseling}}};
\node(2;4) at (-6, -2.5) {%
\parbox{0.3\linewidth}{%
\begin{itemize}[leftmargin=1.5em]
\item Counselor conducts HIV risk assessment.
\item Counsellor negotiates personalized risk reduction plan.
\end{itemize}
}};
\node[draw=white](2;5) at (-6, -4.5) {%
\parbox{0.3\linewidth}{%
\textbf{Counselor Gives Test Results }}};
\node(2;6) at (-6, -5.5) {%
\parbox{0.3\linewidth}{%
Counselor delivers HIV test results and interprets meaning
}};
\node[draw=white](2;7) at (-6, -7) {%
\parbox{0.3\linewidth}{%
\textbf{Post-Test Support/Referral}}};
\node(2;8) at (-6, -8.25) {%
\parbox{0.3\linewidth}{%
Counselor provides emotional support, reinforces risk-reduction plan, and if positive, referral to treatment and care and reinforces importance of disclosure.
}};
\draw[thick,-stealth] (2;2.east) --(-3, 1.35)--(-3, -1.75)--(-0.5, -1.75) -- (-0.5,-2.25);
\draw[thick,-stealth] (1;2.west) --(3, 1)--(3, -1.75)--(0.5, -1.75) -- (0.5,-2.25);
\draw[thick,-stealth,shorten >= 4pt] (2;4.east)-- (0;2.west);
\draw[thick,-stealth,shorten >= 4pt] (1;4.west)-- (0;2.east);
\draw[thick,-stealth,shorten >= -8pt] (1;4.south)-- (1;5.north);
\draw[thick,-stealth,shorten >= -8pt] (1;6.south)-- (1;7.north);
\draw[thick,-stealth,shorten >= -8pt] (2;4.south)-- (2;5.north);
\draw[thick,-stealth,shorten >= -8pt] (2;6.south)-- (2;7.north);
\end{tikzpicture}
\end{center}
\end{document}
我的问题是,我该如何改进代码,以及是否有更好的包可用于实现相同的图表。我尝试阅读有关tree
和forest
包的信息,但无法使用它们。所以我对它进行了某种蛮力处理。由于样式应用于所有节点,因此我也遇到了节点问题。换句话说,要使某些东西取消装箱,我必须将其绘制为白色。
这是我想复制的另一个示例,尽管这不是重点。颜色等无关紧要。
答案1
使用positioning
库来放置节点和一些样式,您的代码可以简化:
\documentclass[border=4pt]{standalone}
\usepackage{enumitem}
\usepackage{tikz}
\usetikzlibrary{positioning}
\setlist[itemize,1]{leftmargin=*,topsep=0pt}
\begin{document}
\begin{tikzpicture}[
mybox/.style={
draw,
text width=5.7cm,
anchor=north
},
nobox/.style={
text width=5.7cm,
align=center,
anchor=north,
font=\bfseries
},
arr/.style={
->,
line width=0.8pt
},
node distance=1cm and 2cm,
>=latex
]
% The left part
\node[mybox]
(left1)
{\textbf{Voluntary Counseling Testing} \\
Average time=1 hour
};
\node[mybox,below=of left1]
(left2)
{\begin{itemize}
\item Some text
\item Some text
\item Some text
\item Some text
\end{itemize}
};
\node[nobox,below=of left2]
(left3)
{HIV Risk reduction Counseling};
\node[mybox,below=0pt of left3]
(left4)
{\begin{itemize}
\item Some text
\item Some text
\item Some text
\item Some text
\end{itemize}
};
\node[nobox,below=of left4]
(left5)
{Counselor gives test results};
\node[mybox,below=0pt of left5]
(left6)
{Some test text goes here for the example};
\node[nobox,below=of left6]
(left7)
{Post-Test Support/Referal};
\node[mybox,below=0pt of left7]
(left8)
{Some test text goes here for the example};
% the middle part
\node[right=of left1,text width=5.5cm]
(middle1) {};
\node[nobox,below=2.5cm of middle1]
(middle2)
{For Patients Who Opt Out};
\node[mybox,below=0pt of middle2]
(middle3)
{\begin{itemize}
\item Some text
\item Some text
\item Some text
\item Some text
\end{itemize}
};
\node[nobox,below=of middle3]
(middle4)
{Specimen Collected};
\node[mybox,below=0pt of middle4]
(middle5)
{Some test text goes here for the example};
\node[nobox,below=of middle5,text width=7cm]
(middle6)
{Further testing performed if indicated};
\node[mybox,below=0pt of middle6]
(middle7)
{Some test text goes here for the example};
% the right part
\node[mybox,right=of middle1]
(right1)
{\textbf{Provide Initiated Counseling Testing} \\
Routine Offer Average time=20--20 minutes
};
\node[mybox,below=of right1]
(right2)
{\begin{itemize}
\item Some text
\item Some text
\item Some text
\item Some text
\end{itemize}
};
\node[nobox,below=of right2]
(right3)
{HIV Risk reduction Counseling};
\node[mybox,below=0pt of right3]
(right4)
{\begin{itemize}
\item Some text
\item Some text
\item Some text
\item Some text
\end{itemize}
};
\node[nobox,below=of right4]
(right5)
{Counselor gives test results};
\node[mybox,below=0pt of right5]
(right6)
{Some test text goes here for the example};
\node[nobox,below=of right6]
(right7)
{Counselor gives test results};
\node[mybox,below=0pt of right7]
(right8)
{Some test text goes here for the example};
%the arrows
\path[arr]
(left4) edge (left5)
(left6) edge (left7)
(right4) edge (right5)
(right6) edge (right7)
(left4) edge (middle5.west)
(right4) edge (middle5.east);
\draw[arr]
(left2.east) --
++(20pt,0pt) |-
([shift={(-20pt,15pt)}]middle4.north) --
([shift={(-20pt,0pt)}]middle4.north);
\draw[arr]
(right2.west) --
++(-20pt,0pt) |-
([shift={(20pt,15pt)}]middle4.north) --
([shift={(20pt,0pt)}]middle4.north);
\end{tikzpicture}
\end{document}
对于其他结构,可以使用以下选项forest
(图片太模糊,所以我不得不猜测想要的方面):
\documentclass[border=4pt]{standalone}
\usepackage{forest}
\forestset{
frbox/.style={
draw=cyan!20,
fill=#1,
line width=1pt,
anchor=north,
text width=4cm,
minimum height=5ex
},
leftn/.style={
text width=#1,
draw=cyan,
double,
line width=1pt,
align=left,
anchor=west,
minimum height=5ex
}
}
\begin{document}
\begin{forest}
for tree={
if={level>3}{edge path={}}{},
},
[Some Title
[Only Child,frbox=cyan
[Only child,frbox=cyan
[Left child,name=left,frbox=cyan!30
[Left two,leftn=3cm
[Left three,leftn=3.5cm
[Left four,leftn=4cm
[Left five,leftn=4.5cm
[Left six,leftn=5cm
[Left seven,leftn=5.5cm,name=last
]
]
]
]
]
]
]
[Right child,frbox=cyan!30
]
]
]
]
\draw ([xshift=-3pt]left.south) -- ([shift={(-3pt,-5pt)}]last.south west);
\end{forest}
\end{document}