我有带文本的节点,我希望它们像前两个节点一样。如您所见,该解决方案有其缺陷:首先,我必须手动调整缩进,因此无法确定它是否正确对齐;其次,一旦文本变短/变长,它就会停止工作。
梅威瑟:
\documentclass{article}
\usepackage{tikz}
\begin{document}
\begin{tikzpicture}
\node[text width=5.5cm,draw] at (0,0) {\parindent=6.8em How are you?: Fine. \\
\indent Thanks};
\node[text width=5.5cm,draw] at (0,-2) {\parindent=6.8em How are you?: Fine. \\
\indent Thanks \\ Very fine!};
\node[text width=5.5cm,draw] at (0,-4) {\parindent=6.8em How are you today?: Fine. \\
\indent Thanks};
\node[text width=5.5cm,draw] at (0,-6) {\parindent=6.8em Fine?: Yes, Fine. \\
\indent Thanks \\ very fine actually};
\node[text width=5.5cm,draw] at (0,-8) {\parindent=6.8em Fine?:\indent Yes, Fine. \\
\indent Thanks \\ No, not fine};
\node[text width=5.5cm,draw] at (0,-10) {
\makebox[6.8em][l]{How are you?:} Fine. Thank you very much I really appreciate it
};
\end{tikzpicture}
\end{document}
\end{tikzpicture}
\end{document}
有没有更好的方法以这种方式对齐节点中的文本?谢谢。
编辑:文本不应超出节点边界,也不应居中。因此,文本应从左上角开始。
答案1
您可能喜欢以下节点设计 :-)。通过使用包description
中定义的,enumitem
您可以获得以下结果:
\documentclass{article}
\usepackage{enumitem}
\newlist{tikzitem}{description}{1} % <-- defined new list
\setlist[tikzitem]{nosep,
leftmargin=27mm,
style=nextline,
before=\vspace{-0.5\baselineskip}
}
\usepackage{tikz}
\usetikzlibrary{chains} % just for shorter code in MWE
\begin{document}
\begin{tikzpicture}[
node distance = 4mm,
start chain = going below,
every node/.style = {draw, text width=56mm, align=left,
inner xsep=1mm, on chain}
]
\node {\begin{tikzitem}
\item[How are you?] Fine.\par
Thanks
\end{tikzitem}};
\node {\begin{tikzitem}
\item[How are you?] Fine.\par
Thanks\par
Very fine!
\end{tikzitem}};
\node {\begin{tikzitem}
\item[How are you today?]
Fine.\par
Thanks
\end{tikzitem}};
\node {\begin{tikzitem}
\item[Fine?] Yes, Fine.\par
Thanks\par
very fine actually
\end{tikzitem}};
\node {\begin{tikzitem}
\item[Fine?] Yes, Fine.\par
Thanks\par
No, not fine
\end{tikzitem}};
\node {\begin{tikzitem}
\item[How are you?] Fine. Thank you very much I really appreciate it
\end{tikzitem}};
\end{tikzpicture}
\end{document}
编辑:
对于更短的代码,您可以定义 nrw 命令,例如\Item
:
\newcommand\Item[2]{\begin{description}[nosep,
leftmargin=27mm,
style=nextline,
before=\vspace{-0.5\baselineskip}
]
\item[#1] #2
\end{description}
}
然后使用它节点:
\node {\Item{How are you?}{Fine.\par
Thanks}};
答案2
您可以将 放在minipage
TikZ 节点内。因此,您可以自由使用所有 TeX/LaTeX 命令,尤其是缩进。
\documentclass{article}
\usepackage{tikz}
\begin{document}
\begin{tikzpicture}
\node[draw,fill=yellow!30,inner sep=3mm]{
\begin{minipage}{4cm}
How are you? Fine!\\
\noindent\phantom{How are you?} Thank you!
\end{minipage}
};
\end{tikzpicture}
\end{document}
答案3
好吧,我绝对不是专家,但我很乐意尝试提供帮助(祝版主选举好运,我投票了)。
\documentclass{article}
\usepackage{tikz}
\begin{document}
\section{Proposal}
% \makebox[width][position]{text}
\begin{tikzpicture}
\node[text width=5.5cm,draw] at (0,0) {
\makebox[6.8em][l]{How are you?:} Fine. \\
\makebox[6.8em][r]{} Thanks
};
\node[text width=5.5cm,draw] at (0,-2) {
\makebox[6.8em][l]{How are you?:} Fine. \\
\makebox[6.8em][r]{} Thanks \\
\makebox[6.8em][r]{} Very Fine! \\
};
\node[text width=5.5cm,draw] at (0,-4) {
\makebox[6.8em][l]{How are you today?:} Fine. \\
\makebox[6.8em][r]{} Thanks
};
\end{tikzpicture}
\section{Original}
\begin{tikzpicture}
\node[text width=5.5cm,draw] at (0,0) {\parindent=6.8em How are you?: Fine. \\
\indent Thanks};
\node[text width=5.5cm,draw] at (0,-2) {\parindent=6.8em How are you?: Fine. \\
\indent Thanks \\ Very fine!};
\node[text width=5.5cm,draw] at (0,-4) {\parindent=6.8em How are you today?: Fine. \\
\indent Thanks};
\node[text width=5.5cm,draw] at (0,-6) {\parindent=6.8em Fine?: Yes, Fine. \\
\indent Thanks \\ very fine actually};
\node[text width=5.5cm,draw] at (0,-8) {\parindent=6.8em Fine?:\indent Yes, Fine. \\
\indent Thanks \\ No, not fine};
\end{tikzpicture}
\end{document}