TikZ:将标题放入节点

TikZ:将标题放入节点

我想将图形的标题放入我的节点中,例如

\usepackage{tikz}
\usepackage{subcaption}

\begin{figure}
    \begin{subfigure}[t]{\textwidth}
        \begin{tikzpicture}
            \draw (0,0) node {\caption{This is the caption}\label{fig}};
        \end{tikzpicture}
    \end{subfigure}
\end{figure}

有可能做到这一点吗?

答案1

仅定义text width节点:

\documentclass{article}
\usepackage{tikz}
\usepackage{subcaption}

\begin{document}
\begin{figure}
    \begin{subfigure}[t]{\textwidth}
        \begin{tikzpicture}
            \draw (0,0) node[text width=\linewidth] {\caption{This is the caption}\label{fig}};
        \end{tikzpicture}
    \end{subfigure}
\end{figure}

在此处输入图片描述

笔记:标题只能出现在文本中,或者在支持写段落的环境中,如parbox,,minipage...

如果你定义节点的文本宽度,那么节点的内容就具有段落特征

附录 节点更美观的示例:

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{shadows}
\usepackage{subcaption}

%--- show page layout, only for test, don't use in real document!
\usepackage{showframe}
\renewcommand\ShowFrameLinethickness{0.15pt}
\renewcommand*\ShowFrameColor{\color{red}}
%---------------------------------------------------------------%

\begin{document}
\begin{figure}
    \begin{subfigure}[t]{\textwidth}
        \begin{tikzpicture}
            \draw (0,0) node[
                draw=yellow!60,                 % border around node with defined color
                semithick,                      % border line thicknes
                fill=yellow!30,                 % color of node's fill 
                text width=\linewidth-          % with  of node, 
                                                % considered are inner xsep
                                                % and shadow xhift, with this it 
                                                % not spill out right text border
                                      2*\pgfkeysvalueof{/pgf/inner xsep}-4pt,
                text=black,                     % text color
                drop shadow={fill=orange,       % shodows with added defined option: 
                                                % color of the fill
                                                %% default values are:
                                                % shadow scale=1, 
                                                % shadow xshift=.5ex, shadow yshift=-.5ex,
                                                % opacity=.5, fill=black!50, every shadow
                            shadow xshift= 4pt, % shift to the right
                            shadow yshift=-4pt} % shift down (or with sign - up)
                             ] {\caption{This is the caption}\label{fig}};
        \end{tikzpicture}
    \end{subfigure}
\end{figure}
\end{document}

在此处输入图片描述

如果想要更奇特的风格,您应该考虑包装tcolorbox

答案2

尝试了一个小时后,我还是没能找到答案,5 分钟后我想到了一个解决方案...不过,它在这里:

\usepackage{tikz}
\usepackage{subcaption}

\newcommand{\nodecaption}[2]{\subcaptionbox{#1\label{#2}}{\phantom{(X)~#1}}}

\begin{figure}
    \begin{subfigure}[t]{\textwidth}
        \begin{tikzpicture}
            \draw (0,0) node {\nodecaption{This is the caption}{fig}};
        \end{tikzpicture}
    \end{subfigure}
\end{figure}

虽然远非最佳,但它确实发挥了作用。

相关内容