问题
我想在 tikz 节点内放置一个 minipage 环境。我知道这已经通过 align=left 命令完成了,但如果您将节点的最小宽度设置为 \linewidth,则文本将左对齐,但周围有空白填充(取决于您包含的文本量)
问题
当我将 minipage 环境的宽度设置为 \linewidth 时,minipage 会抓取页面上线的宽度而不是节点的宽度。
示例代码
\newcommand{\warning}[1]{%
% Define box and box title style
\tikzstyle{mybox} = [align=left,draw=red, very thick,
rectangle, rounded corners, inner sep=10pt, inner ysep=10pt,maximum width=\linewidth]
\tikzstyle{fancytitle} =[fill=red, text=white,rounded corners]
\begin{tikzpicture}
\node [mybox] (box){%
\begin{minipage}{1\textwidth}
#1
\end{minipage}
};
\node[fancytitle, right=10pt] at (box.north west) {Note:};
\end{tikzpicture}
}%
改编自http://www.texample.net/tikz/examples/boxes-with-text-and-math/
答案1
像这样?
\documentclass{article}
\usepackage{tikz,lipsum,showframe}
\tikzset{mybox/.style = {text width=\dimexpr\linewidth-20pt-\pgflinewidth\relax,align=justify,draw=red,
very thick, rectangle, rounded corners, inner sep=10pt, inner ysep=10pt},
fancytitle/.style ={fill=red, text=white,rounded corners}
}
\begin{document}
\noindent
\begin{tikzpicture}
\node [mybox] (box){%
\lipsum[1]
};
\node[fancytitle, right=10pt] at (box.north west) {Note:};
\end{tikzpicture}
\end{document}
按照要求明确说明minipage
:
\documentclass{article}
\usepackage{tikz,lipsum,showframe}
\tikzset{mybox/.style = {align=center,draw=red,
very thick, rectangle, rounded corners, inner sep=10pt, inner ysep=10pt},
fancytitle/.style ={fill=red, text=white,rounded corners}
}
\begin{document}
\noindent
\begin{tikzpicture}
\node [mybox] (box){%
\begin{minipage}{\dimexpr\linewidth-20pt-\pgflinewidth\relax}
\lipsum[1]
\end{minipage}%
};
\node[fancytitle, right=10pt] at (box.north west) {Note:};
\end{tikzpicture}
\end{document}