tikz - 文本未垂直对齐以及如何使文本居中?

tikz - 文本未垂直对齐以及如何使文本居中?

您可以在图中看到文本“重建”和“预测”未垂直对齐。我该如何解决这个问题?此外,是否可以将文本置于图的中间?在 powerpoint 中,我会创建一个长度相同的文本框,然后将文本置于其中的中心。我在这里该怎么做?

\documentclass{article}
\usepackage{float}
\usepackage{subfig}
\usepackage{tikz}% loads graphicx
\usetikzlibrary{calc}

\begin{document}

\def\imagewidth{0.12\textwidth}
\begin{figure}[t!]
\centering
\subfloat{
\begin{tikzpicture}[remember picture]
    \node[anchor=north west,inner sep=0] (image1) at (0,0) {\includegraphics[width=\imagewidth]{example-image-a}};
\end{tikzpicture}}
\subfloat{
\begin{tikzpicture}[remember picture]
    \node[anchor=north west,inner sep=0] (image2) at (0,0) {\includegraphics[width=\imagewidth]{example-image-a}};
\end{tikzpicture}}
\subfloat{
\begin{tikzpicture}[remember picture]
    \node[anchor=north west,inner sep=0] (image3) at (0,0) {\includegraphics[width=\imagewidth]{example-image-a}};
\end{tikzpicture}}
\subfloat{
\begin{tikzpicture}[remember picture]
    \node[anchor=north west,inner sep=0] (image4) at (0,0) {\includegraphics[width=\imagewidth]{example-image-a}};
\end{tikzpicture}}
\subfloat{
\begin{tikzpicture}[remember picture]
    \node[anchor=north west,inner sep=0] (image5) at (0,0) {\includegraphics[width=\imagewidth]{example-image-a}};
\end{tikzpicture}}
\subfloat{
\begin{tikzpicture}[remember picture]
    \node[anchor=north west,inner sep=0] (image6) at (0,0) {\includegraphics[width=\imagewidth]{example-image-a}};
\end{tikzpicture}}
\subfloat{
\begin{tikzpicture}[remember picture]
    \node[anchor=north west,inner sep=0] (image7) at (0,0) {\includegraphics[width=\imagewidth]{example-image-a}};
\end{tikzpicture}}
\subfloat{
\begin{tikzpicture}[remember picture]
    \node[anchor=north west,inner sep=0] (image8) at (0,0) {\includegraphics[width=\imagewidth]{example-image-a}};
\end{tikzpicture}}

\begin{tikzpicture}[overlay,remember picture]
\begin{scope}

    \draw[line width=0.3mm] ([xshift=0.25cm,yshift=0.1cm]image1.north west) -- ([xshift=-0.25cm,yshift=0.1cm]image3.north east);
    \node[anchor=south west, fill=white] at ([xshift=0.70cm,yshift=0.15cm]image1.north west) {Reconstruction};
    
    \draw[line width=0.3mm] ([xshift=0.25cm,yshift=0.1cm]image4.north west) -- ([xshift=-0.25cm,yshift=0.1cm]image8.north east);
    \node[anchor=south west, fill=white] at ([xshift=2.5cm,yshift=0.15cm]image4.north west) {Forecasting};

\end{scope}
\end{tikzpicture}
\end{figure}

    
\end{document}

在此处输入图片描述

答案1

对于第一个问题(垂直对齐),\strut在文本中添加一个,这样它们两个的高度就相同了。对于第二个问题,将文本放在节点midway路径中。

此外,我认为您不需要scope在最后一个中添加tikzpicture,如果您不想要单独的标题,最好将所有内容放在同一个中tikzpicture

\documentclass{article}
\usepackage{float}
\usepackage{subfig}
\usepackage{tikz}% loads graphicx
\usetikzlibrary{calc}

\begin{document}

\def\imagewidth{0.12\textwidth}
\begin{figure}[t!]
\centering
\subfloat{
\begin{tikzpicture}[remember picture]
    \node[anchor=north west,inner sep=0] (image1) at (0,0) {\includegraphics[width=\imagewidth]{example-image-a}};
\end{tikzpicture}}
\subfloat{
\begin{tikzpicture}[remember picture]
    \node[anchor=north west,inner sep=0] (image2) at (0,0) {\includegraphics[width=\imagewidth]{example-image-a}};
\end{tikzpicture}}
\subfloat{
\begin{tikzpicture}[remember picture]
    \node[anchor=north west,inner sep=0] (image3) at (0,0) {\includegraphics[width=\imagewidth]{example-image-a}};
\end{tikzpicture}}
\subfloat{
\begin{tikzpicture}[remember picture]
    \node[anchor=north west,inner sep=0] (image4) at (0,0) {\includegraphics[width=\imagewidth]{example-image-a}};
\end{tikzpicture}}
\subfloat{
\begin{tikzpicture}[remember picture]
    \node[anchor=north west,inner sep=0] (image5) at (0,0) {\includegraphics[width=\imagewidth]{example-image-a}};
\end{tikzpicture}}
\subfloat{
\begin{tikzpicture}[remember picture]
    \node[anchor=north west,inner sep=0] (image6) at (0,0) {\includegraphics[width=\imagewidth]{example-image-a}};
\end{tikzpicture}}
\subfloat{
\begin{tikzpicture}[remember picture]
    \node[anchor=north west,inner sep=0] (image7) at (0,0) {\includegraphics[width=\imagewidth]{example-image-a}};
\end{tikzpicture}}
\subfloat{
\begin{tikzpicture}[remember picture]
    \node[anchor=north west,inner sep=0] (image8) at (0,0) {\includegraphics[width=\imagewidth]{example-image-a}};
\end{tikzpicture}}

\begin{tikzpicture}[overlay,remember picture]
    \draw[line width=0.3mm] ([xshift=0.25cm,yshift=0.1cm]image1.north west) --
                            ([xshift=-0.25cm,yshift=0.1cm]image3.north east)node[midway,above] {\strut Reconstruction};
    \draw[line width=0.3mm] ([xshift=0.25cm,yshift=0.1cm]image4.north west) --
                            ([xshift=-0.25cm,yshift=0.1cm]image8.north east)node[midway,above] {\strut Forecasting};
\end{tikzpicture}
\end{figure}
\end{document}

在此处输入图片描述

更新:如果不分开标题,您可以执行以下操作(输出几乎相同):

\documentclass{article}
\usepackage{tikz}

\begin{document}
\begin{figure}[t!]\centering
\def\imagewidth{0.12\textwidth}
\def\sep{0.5mm}
\begin{tikzpicture}
  \node              (image1) at (0,0)                      {\includegraphics[width=\imagewidth]{example-image-a}};
  \node[anchor=west] (image2) at ([xshift=\sep]image1.east) {\includegraphics[width=\imagewidth]{example-image-a}};
  \node[anchor=west] (image3) at ([xshift=\sep]image2.east) {\includegraphics[width=\imagewidth]{example-image-a}};
  \node[anchor=west] (image4) at ([xshift=\sep]image3.east) {\includegraphics[width=\imagewidth]{example-image-a}};
  \node[anchor=west] (image5) at ([xshift=\sep]image4.east) {\includegraphics[width=\imagewidth]{example-image-a}};
  \node[anchor=west] (image6) at ([xshift=\sep]image5.east) {\includegraphics[width=\imagewidth]{example-image-a}};
  \node[anchor=west] (image7) at ([xshift=\sep]image6.east) {\includegraphics[width=\imagewidth]{example-image-a}};
  \node[anchor=west] (image8) at ([xshift=\sep]image7.east) {\includegraphics[width=\imagewidth]{example-image-a}};
  \draw[line width=0.3mm] ([xshift= 0.25cm,yshift=0.1cm]image1.north west) --
                          ([xshift=-0.25cm,yshift=0.1cm]image3.north east) node[midway,above] {\strut Reconstruction};
  \draw[line width=0.3mm] ([xshift= 0.25cm,yshift=0.1cm]image4.north west) --
                          ([xshift=-0.25cm,yshift=0.1cm]image8.north east) node[midway,above] {\strut Forecasting};
\end{tikzpicture}
\end{figure}
\end{document}

答案2

您的问题不完全清楚:

  • 为什么每张图片都使用\subfloat?每张图片都有自己的标题吗?
  • subfloat不用s就可以绘制图像吗?

在 MWE 中,所有图像或一个图像位于下方tikzpicture。使用chains库来定位带有图像的节点。带有的图像由 确定\setkeys{Gin}{...},通过这两种措施,代码相当短:

\documentclass{article}
\usepackage{tikz}      % loads graphicx
\usetikzlibrary{calc, chains,
                positioning}

\begin{document}
    \begin{figure}[!ht]
\setkeys{Gin}{width=0.12\linewidth}
\centering
\begin{tikzpicture}[
    node distance = 1mm and 1mm,
      start chain = going right,
       arr/.style = {thick, shorten <=2mm, shorten >=2mm},
every node/.style = {inner sep=0pt, outer ysep=2mm, on chain}
                    ]
    \node (n1)  {\includegraphics{example-image-a}};
    \node       {\includegraphics{example-image-a}};
    \node (n3)  {\includegraphics{example-image-a}};
    \node (n4)  {\includegraphics{example-image-a}};
    \node       {\includegraphics{example-image-a}};
    \node       {\includegraphics{example-image-a}};
    \node (n7)  {\includegraphics{example-image-a}};
\draw[arr]  (n1.north west) -- node[above] {Reconstruction}    (n3.north east);
\draw[arr]  (n4.north west) -- node[above] {Forecasting}    (n7.north east);
    \end{tikzpicture}
\end{figure}

在此处输入图片描述

相关内容