tikz 图片库

tikz 图片库

我需要一种在 tikzpicture 中制作图像库或 \node... 条目的简单方法,每个条目都有一个标题,遵循一些标准,例如N x P,其中N是水平和垂直方向上的图像/条目数P。目前,我使用两个变量,包含两个条目之间的水平空间中间到中间,垂直方向也是如此。这样做的缺点是,如果我必须更改条目出现的顺序,我需要对名称及其标题内容进行更改,而无需触及其余行,因为它们包含标题使用的坐标和标签。所以我希望能够通过简单地附加条目(如'\includegraphics...'或'\ticks ...')及其标题(如 mwe 中所示)来获得相同的结果,而不必担心标签和坐标。这有点像 \matrix 对文本所做的,但这里是针对图像-标题对。

这是我的妈妈:

\documentclass[a4paper]{article}
\usepackage[verbose,vmargin=30mm,hmargin=20mm]{geometry}
\usepackage{tikz}
\usetikzlibrary{positioning,calc}

\tikzset{
  caption/.style = {anchor=north, yshift=-1mm, align=center},
  img/.style = {inner sep=0pt,anchor=north}}

\usepackage{graphicx}
\usepackage{tikzpagenodes}
\setkeys{Gin}{keepaspectratio}

% following command to force the entry to be of size #1x#2 and the image into
% the \makebox be at the bottom.
% Using \makebox will allow captions to be horizontally aligned if #1x#2 is same
% for all entries of the gallery.

\newcommand{\boximg}[3]{
  \makebox(#1,#2)[b]{\includegraphics[width=#1,height=#2]{#3}}}

\setlength{\parindent}{0pt}
\begin{document}
\begin{tikzpicture}[overlay, remember picture]
\newlength\x\setlength\x{.2\textwidth}
\newlength\y\setlength\y{.16667\textheight}

    \node at (current page text area.north west) (titre)
    [font=\huge, align=center,anchor=north west] {{titre 1}};
  %  \draw [line width=1pt, yshift=-2pt] (titre.south west) -- (titre.south east);

    \node  at ([yshift=-8mm] 0.5\x,0 |- titre.south)
    (im1) [img] {\boximg{30mm}{30mm}{example-image}};
    \node at (im1.south) [caption] {my image 1};

    \node  at (1.5\x,0 |- im1.north)
    (im2) [img] {\boximg{30mm}{30mm}{example-image}};
    \node at (im2.south) [caption] {my image 2};

    \node  at (2.5\x,0 |- im1.north)
    (im3) [img] {\boximg{30mm}{30mm}{example-image}};
    \node at (im3.south) [caption] {my image 3};

    \node  at (3.5\x,0 |- im1.north)
    (im4) [img] {\boximg{30mm}{30mm}{example-image}};
    \node at (im4.south) [caption] {my image 4};

    \node  at (4.5\x,0 |- im1.north)
    (im5) [img] {\boximg{30mm}{30mm}{example-image}};
    \node at (im5.south) [caption] {my image 5};

    \node  at ([yshift=-\y] 0.5\x,0 |- im1.north)
    (tikz1) [img] {\boximg{30mm}{30mm}{example-image}};
    \node at (tikz1.south) [caption] {my tikz 1};
\end{tikzpicture}
\end{document}

答案1

如果我正确理解了您的意图,那么我认为您不需要 tikz魔法来形成图库。它可以按以下方式组织\subfloats

\documentclass[a4paper]{article}
\usepackage[verbose,vmargin=30mm,hmargin=20mm]{geometry}
%---------------- Show page layout. Don't use in a real document!
\usepackage{showframe}
\renewcommand\ShowFrameLinethickness{0.15pt}
\renewcommand*\ShowFrameColor{\color{red}}
%---------------------------------------------------------------%
\usepackage{subcaption}
\usepackage{graphicx}

\setlength{\parindent}{0pt}
\begin{document}
    \begin{figure}[!ht]
    \setkeys{Gin}{width=0.18\linewidth}
    \captionsetup[subfigure]{skip=0.5ex,
                             belowskip=1ex,
                             labelformat=simple}
    \renewcommand\thesubfigure{}

\subfloat[image 1]{\includegraphics{example-image-duck}}
\hfill
\subfloat[image 2]{\includegraphics{example-image-duck}}
\hfill
\subfloat[image 3]{\includegraphics{example-image-duck}}
\hfill
\subfloat[image 4]{\includegraphics{example-image-duck}}
\hfill
\subfloat[image 5]{\includegraphics{example-image-duck}}

\medskip
\subfloat[image 6]{\includegraphics{example-image-duck}}
\hfill
\subfloat[image 7]{\includegraphics{example-image-duck}}
\hfill
\subfloat[image 8]{\includegraphics{example-image-duck}}
\hfill
\subfloat[image 9]{\includegraphics{example-image-duck}}
\hfill
\subfloat[image 10]{\includegraphics{example-image-duck}}
    \end{figure}
\end{document}

在此处输入图片描述

(红线表示页面布局)

编辑: 上述解决方案预计每行图像都是满的。在某些情况下,当最后一行图像较少时,例如只有三张,应该左对齐,则\hfill必须用固定宽度替换命令。例如,使用可以\sep定义为的新命令\newcommand\sep{\hspace{0.025\linewidth}}。此类图库的示例如下:

\documentclass[a4paper]{article}
\usepackage[verbose,vmargin=30mm,hmargin=20mm]{geometry}


\usepackage{subcaption}
\usepackage{graphicx}

\setlength{\parindent}{0pt}
\begin{document}
    \begin{figure}[!ht]
    \setkeys{Gin}{width=0.18\linewidth}
    \captionsetup[subfigure]{skip=0.5ex,
                             belowskip=1ex,
                             labelformat=simple}
    \renewcommand\thesubfigure{}
    \newcommand\sep{\hspace{0.025\linewidth}}

\subfloat[image 1]{\includegraphics{example-image-duck}}
\sep
\subfloat[image 2]{\includegraphics{example-image-duck}}
\sep
\subfloat[image 3]{\includegraphics{example-image-duck}}
\sep
\subfloat[image 4]{\includegraphics{example-image-duck}}
\sep
\subfloat[image 5]{\includegraphics{example-image-duck}}

\medskip
\subfloat[image 6]{\includegraphics{example-image-duck}}
\sep
\subfloat[image 7]{\includegraphics{example-image-duck}}
\sep
\subfloat[image 8]{\includegraphics{example-image-duck}}
\sep
\subfloat[image 9]{\includegraphics{example-image-duck}}
\sep
\subfloat[image 10]{\includegraphics{example-image-duck}}

\medskip
\subfloat[image 11]{\includegraphics{example-image-duck-portrait}}
\sep
\subfloat[image 12]{\includegraphics{example-image-duck-portrait}}
\sep
\subfloat[image 13]{\includegraphics{example-image-duck-portrait}}
    \end{figure}
\end{document}

在此处输入图片描述

附录:

既然你坚持认为该解决方案必须基于tikzpicture,这里是可能的解决方案之一。它使用 TiZ 库chainpositioning。标题使用label选项:

\documentclass[a4paper]{article}
\usepackage[verbose,vmargin=30mm,hmargin=20mm]{geometry}

\usepackage{graphicx}
\usepackage{tikz}
\usetikzlibrary{chains,
                positioning}

\setlength{\parindent}{0pt}

\begin{document}
    \setkeys{Gin}{width=\linewidth}
    \begin{tikzpicture}[
node distance = 5ex and 0.025\linewidth,
  start chain = A going right,
   box/.style = {text width=0.18\linewidth,
                 inner sep=0pt, outer sep=0pt,
                 label=below:#1,
                 on chain=A}
                        ]
\node[box=image 1]{\includegraphics{example-image-duck}}; % A-1
\node[box=image 2]{\includegraphics{example-image-duck}};
\node[box=image 3]{\includegraphics{example-image-duck}};
\node[box=image 4]{\includegraphics{example-image-duck}};
\node[box=image 5]{\includegraphics{example-image-duck}};

\node[box=image 6,
      below=of A-1]{\includegraphics{example-image-duck}};% A-6
\node[box=image 7]{\includegraphics{example-image-duck}};
\node[box=image 8]{\includegraphics{example-image-duck}};
\node[box=image 9]{\includegraphics{example-image-duck}};
\node[box=image 10]{\includegraphics{example-image-duck}};

\node[box=image 11,
      below=of A-6]{\includegraphics{example-image-duck-portrait}};% A-11
\node[box=image 12]{\includegraphics{example-image-duck-portrait}};
\node[box=image 13]{\includegraphics{example-image-duck-portrait}};
    \end{tikzpicture}
\end{document}

在此处输入图片描述

答案2

我知道您想使用,TiKZ但是使用tcbraster(来自tcolorbox包)您可以执行以下操作:

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

\begin{document}
\begin{tcbraster}[raster columns=4, enhanced, sharp corners, halign title=center, flip title={boxsep=1mm}, blank, colbacktitle=white, coltitle=black]
\foreach \i in {a,b,c,a,b,c,a,b,c,a,b,c,a,b,c}{
\tcbincludegraphics[title=image~\thetcbrasternum]{example-image-\i}}
\end{tcbraster}
\end{document}

在此处输入图片描述

更新

原始代码使用foreach循环只是为了方便。当然,可以用其特定的标题声明每个单独的图像。作为替代方案,也可以使用foreach带有对的循环image-file/caption

至于在两个或多个图形下方添加标题的可能性,使用multicolum元素也是可以的。以下代码显示了这一点。需要更好地调整内部图像大小,但这只是一个使用示例。

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

\begin{document}
\begin{tcbitemize}[raster columns=4, enhanced, sharp corners, halign title=center, flip title={boxsep=1mm}, blank, colbacktitle=white, coltitle=black]
% For each with automaticaly declared captions
\foreach \i in {a,b,c,a,b,c}{
\tcbincludegraphics[title=image~\thetcbrasternum]{example-image-\i}}
% Individual declaration
\tcbincludegraphics[title=Special title]{example-image}
% for each with pairs image-file/caption
\foreach \i/\j in {example-image-a/First, example-image-c/Second, example-image/Third}
{\tcbincludegraphics[title=\j]{\i}}
% Special box with two images with one caption. It needs more work
\tcbitem[raster multicolumn=2, title=Experiment]{\includegraphics[width={.495\linewidth}]{example-image-a}\hfill\includegraphics[width={.495\linewidth}]{example-image-b}}

\end{tcbitemize}
\end{document}

在此处输入图片描述

相关内容