TikZ 矩形分割水平额外空间

TikZ 矩形分割水平额外空间

以下代码在图形左侧和文本右侧都留出了额外空间。有没有办法让分割的矩形紧贴较大的文本或图形?

% !TEX encoding = UTF-8 Unicode
% !TEX TS-program = XeLaTeX

\documentclass{article}

\usepackage{graphicx}
\usepackage{tikz}
\usepackage{multicol}

\usetikzlibrary{shapes.multipart,positioning}

\newsavebox{\testbox}

\begin{document}

\begin{lrbox}{\testbox}
    \begin{minipage}{3in}
        \begin{multicols}{2}
            This is a lot of bla bla bla to see what is happening. And this is more text. and a lot more. And this is more text. and a lot more. And this is more text. and a lot more. 
        \end{multicols}
    And this is more text. and a lot more. And this is more text. and a lot more. And this is more text. and a lot more
    \end{minipage}
\end{lrbox}

\tikz{%
    \node[%
        rectangle split,
        rectangle split parts=2,
        rectangle split horizontal,
        rectangle split part align={top},
        draw,
        line width=3pt
    ](test){%
        \includegraphics[width=2in]{example-image-a}
        \nodepart{two} \usebox{\testbox}.
    };
    \node[above=12pt of test.south,fill=white]{%
        \bfseries\LARGE\color{red} $\Longleftarrow$ EXTRA SPACE? $\Longrightarrow$};
}%

\end{document}

额外空间的示例。

答案1

这与您包含图形的方式有关。这是修复此问题的众多方法之一:将其放入$\vcenter{\hbox{...}}$,这是垂直居中对象(在方程式中)的标准技巧。我的选择并不特别,也不复杂,但似乎有效。至于问题:为什么这里需要这样做?我没有令人信服的答案,除了也许我对必须关心垂直对齐并不感到惊讶。

\documentclass{article}

\usepackage{graphicx}
\usepackage{tikz}
\usepackage{multicol}

\usetikzlibrary{shapes.multipart,positioning}

\newsavebox{\testbox}

\begin{document}

\begin{lrbox}{\testbox}
    \begin{minipage}{3in}
        \begin{multicols}{2}
            This is a lot of bla bla bla to see what is happening. And this is more text. and a lot more. And this is more text. and a lot more. And this is more text. and a lot more. 
        \end{multicols}
    And this is more text. and a lot more. And this is more text. and a lot more. And this is more text. and a lot more
    \end{minipage}
\end{lrbox}

\tikz{%
    \node[%
        rectangle split,
        rectangle split parts=2,
        rectangle split horizontal,
        rectangle split part align={top},
        draw,
        line width=3pt
    ](test){%
        $\vcenter{\hbox{\includegraphics[width=2in]{example-image-a}}}$
        \nodepart{two} \usebox{\testbox}.
    };
}%

\end{document}

在此处输入图片描述

答案2

另一种解决方案是使用包adjustbox

\documentclass{article}
\usepackage[export]{adjustbox}
\usepackage{tikz}
\usetikzlibrary{positioning,
                shapes.multipart}
\usepackage{multicol}
\usepackage{ragged2e}

\newsavebox{\testbox}

\begin{document}

\begin{lrbox}{\testbox}
    \begin{minipage}{3in}\RaggedRight
        \begin{multicols}{2}
            This is a lot of bla bla bla to see what is happening. And this is more text. and a lot more. And this is more text. and a lot more. And this is more text. and a lot more.
        \end{multicols}
    And this is more text. and a lot more. And this is more text. and a lot more. And this is more text. and a lot more
    \end{minipage}
\end{lrbox}

\tikz{%
    \node[%
        rectangle split,
        rectangle split parts=2,
        rectangle split horizontal,
        rectangle split part align={top},
        draw,
        line width=3pt
    ] (test)
    {\includegraphics[width=2in,valign=c]{example-image-a} % with "valign=c" is image baseline moved to its vertical center
      \nodepart{two}  \usebox{\testbox}
    };
}

\end{document}

在此处输入图片描述

答案3

如果您可以接受文本框而不是TikZ节点,tcolorbox则提供一个\tcbsidebyside框,您可以在其中决定哪部分将适应其大小,而另一部分使用可用空间(\textwidth或框width)。

\documentclass{article}
\usepackage[most]{tcolorbox}
\usepackage{multicol}
\usepackage{lmodern}

\begin{document}
\tcbsidebyside[notitle,
    sidebyside adapt=left,
    colback=white,
    enhanced,
    segmentation style=solid,
    sharp corners]
    {%
    \includegraphics[width=3cm]{example-image-a}
    }{%
        \begin{multicols}{2}
            This is a lot of bla bla bla to see what is happening. And this is more text. and a lot more. And this is more text. and a lot more. And this is more text. and a lot more. 
        \end{multicols}
    And this is more text. and a lot more. And this is more text. and a lot more. And this is more text. and a lot more
    }

\tcbsidebyside[notitle,
    sidebyside adapt=left,
    colback=white,
    enhanced,
    segmentation style=solid,
    sharp corners]
    {%
    \includegraphics[width=5cm]{example-image-a}
    }{%
    And this is more text. and a lot more. And this is more text. and a lot more. And this is more text. and a lot more
    }
\end{document}

在此处输入图片描述

相关内容