将文本基线与 TikZ 图片对齐,前在北,后在东

将文本基线与 TikZ 图片对齐,前在北,后在东

好奇你会如何使用 TikZ 来完成以下操作:

在此处输入图片描述

我想尝试这样的事情:

Here is some text to wrap
\begin{tikzpicture}[baseline={([yshift={-\ht\strutbox}]current bounding box.north)}, execute at end picture={baseline=(current bounding box.east)}]
\draw[red] (0,0) rectangle (3,2);
\end{tikzpicture}
around this picture

但它肯定只是做前者。

PS. 另外,如果右侧的文本的中间高度可以位于边界框的中间高度,那么这将是首选(而不是文本的基线位于“东”设置给出的边界框的中间高度)

答案1

\documentclass[border=1cm]{standalone}
\usepackage{tikz}

\begin{document} 
    

\begin{tikzpicture}[baseline={([yshift={-\ht\strutbox}]current bounding box.north)}, execute at end picture={baseline=(current bounding box.east)}]
    \draw[red] (0,0) rectangle (3,2);
    \node[xshift=-2.2cm] at (0,1.8){Here is some text to wrap};
    \node[xshift=1.8cm] at (3,1){around this picture
    };
\end{tikzpicture}

\end{document}

在此处输入图片描述

或者你的意思是把它包装在文本中:

\documentclass{article}
\usepackage{wrapfig}
\usepackage{lipsum}
\usepackage{tikz}

\begin{document}

\lipsum[1]
\begin{wrapfigure}{r}{3cm}
    \centering
    \begin{tikzpicture}[baseline={([yshift={-\ht\strutbox}]current bounding box.north)}, execute at end picture={baseline=(current bounding box.east)}]
        \draw[red] (0,0) rectangle (3,2);
    \end{tikzpicture}
\end{wrapfigure}
\lipsum[1]
\end{document}

在此处输入图片描述

答案2

欢迎来到 StackExchange!

我已经无数次使用过这种技巧。首先,画出你喜欢的东西。然后使用current bounding box你画的东西迄今为止放置文本或其他任何内容。然后,您可以继续添加更多元素(如果您愿意),而不会干扰您已经绘制的内容。

\documentclass{article}

\usepackage{tikz}

\usetikzlibrary{positioning} %% very handy

\begin{document}

\begin{tikzpicture}
    %% Draw anything you like; in this case a simple rectangle:
    \draw[red] (0,0) rectangle (3,2);
    %%
    %% Use the current bounding box to place the text anywhere you like:
    \node[inner ysep=0pt,%% Top of text will align with top of current bounding box
        left=0pt of current bounding box.north west,%% Adjust to suit; use tikz positioning library
        anchor=north east
    ] {Here is some text to wrap};
    \node[right=0pt of current bounding box.east,%% Use tikz positioning library
        anchor=west %% Vertically centers the content of the node
    ]{around this picture};
    %%
    %% Now you can draw anything else you like here, for example:
    \node[draw] at (1,-.4) {More stuff};
\end{tikzpicture}

\end{document}

在此处输入图片描述

相关内容