平铺版本tabular

平铺版本tabular

借助以下答案TikZ:绘制带有图片的正六边形,我为儿童游戏创建了一些方块:

在此处输入图片描述

为了节省打印 + 卡片的成本,我想在 [a4] 纸的缝隙中放入另外 3 个六角形。

生成的代码如下所示:

\tile{Lake}{lake.jpg}{1.2}{Starts on Board}{--}{\food}\hfill
\tile{Forest}{forest.jpg}{1.6}{Starts on Board}{--}{\wood}\\

\tile{Forest}{forest.jpg}{1.6}{Starts on Board}{--}{\wood}\hfill
\tile{Forest}{forest.jpg}{1.6}{Starts on Board}{--}{\wood}\\

\tile{Baker}{baker.jpg}{7}{Build on Clearing}{\wood\wood\person}{\convert{\grain}{\food}}\hfill
\tile{Butcher}{butcher.jpg}{10}{Build on Clearing}{\wood\wood\person}{\convert{\sheep}{\food}}\\

\tile{Baker}{baker.jpg}{7}{Build on Clearing}{\wood\wood\person}{\convert{\grain}{\food}}\hfill
\tile{Butcher}{butcher.jpg}{10}{Build on Clearing}{\wood\wood\person}{\convert{\sheep}{\food}}\\

我可以添加一个额外的“位置”参数,\tile并用 11 个位置循环实例化它,但当我更改图块数量时(例如将 3 个森林更改为 4 个森林),这将很难维护。所以:我怎样才能将位置信息与图块内容的规范分开?

编辑:按要求提供完整代码。(图块略有变化,但不会影响问题。)

\documentclass[a4paper]{article}
\usepackage[margin=2mm]{geometry}
\usepackage{tikz,nopageno}
\usepackage{wasysym}

\usetikzlibrary{fit,backgrounds}

\setlength{\parskip}{0mm}

\newcommand{\highlight}[2]{%
\begin{tikzpicture}[baseline = (text.base)]
  \node[inner sep=0pt] (text) {#2};
  \begin{pgfinterruptboundingbox}
    \begin{pgfonlayer}{background}
    \node[fit=(text), rounded corners, fill=white, opacity=#1, draw=none] {};
    \end{pgfonlayer}
  \end{pgfinterruptboundingbox}
\end{tikzpicture}%
}

% General macro to draw a shape filled with a bitmap
\newcommand\fillshape[3]{ % #1 = shape, #2 = filename of texture, #3 = includegraphics options
    \begin{scope}
        \clip #1;
        \node {\includegraphics[#3]{#2}};
    \end{scope}
    \draw[line width=0.5mm] #1;
}

% Particularization for hexagonal shape
\newcommand\fillhexagon[2][]{% #1 (optional) = includegraphics options, #2 = filename of texture
    \fillshape{(0:4) -- (60:4) -- (120:4) -- (180:4) -- (240:4) -- (300:4) -- cycle}{#2}{#1}
}

\newcommand\tile[6]{
\begin{tikzpicture}
    \fillhexagon[scale=#3]{#2};
    \node at (0,2.4) {\highlight{0.65}{\LARGE\bf #1}};    
    \node at (0,0.7) {\highlight{1}{#6}};   
    \draw [rounded corners, fill=white, opacity=1] (-2,-0.6)--(2,-0.6)--(2,-2.5)--(-2,-2.5)--cycle; 
    \node at (0,-1.05) {#4};       
    \node at (0,-1.85) {#5};       
\end{tikzpicture}
}

\newcommand\image[2]{$\vcenter{\hbox{\includegraphics[scale=#1]{#2}}}$}

\newcommand\wood{\image{0.22}{wood.png}}
\newcommand\food{\image{0.23}{bowl.jpg}}
\newcommand\person{\image{0.25}{peasant.jpg}}
\newcommand\grain{\image{0.5}{grain2.jpg}}
\newcommand\sheep{\image{0.05}{sheep.png}}
\newcommand\stone{\image{0.35}{stone.png}}
\newcommand\ale{\image{0.12}{ale.png}}
%\newcommand\cloth{\image{0.2}{cloth.png}}
\newcommand\paper{\image{0.1}{paper.png}}
\newcommand\gold{\image{0.7}{gold.jpg}}
\newcommand\marble{\image{0.2}{marble.jpg}}

\newcommand\TO{\Huge$\rightarrow$}

\newcommand*{\vpointer}{$\vcenter{\hbox{\ \scalebox{2}{$\rightarrow$}}}$}

\newcommand\convert[2]{#1\vpointer\, #2}

\begin{document}
\tile{Lake}{lake.jpg}{1.2}{Starts on Board}{--}{\food}\hfill
\tile{Field}{field2.jpg}{1.5}{Replaces Clearing}{\person\person\person\person}{\grain}\\

\tile{Forest}{forest2.jpg}{0.4}{Starts on Board}{--}{\wood}\hfill
\tile{Mountain}{mountain2.jpg}{0.3}{Starts on Board}{--}{--}\\

\tile{Baker}{baker.jpg}{7}{Build on Clearing}{\wood\wood\person}{\convert{\grain}{\food}}\hfill
\tile{Butcher}{butcher.jpg}{10}{Build on Clearing}{\wood\wood\person}{\convert{\sheep}{\food}}\\

\tile{Grazing}{grazing.jpg}{0.3}{Replaces Hill}{\person}{\sheep}\hfill
\tile{Pasture}{sheepfield.jpg}{2.1}{Replaces Clearing}{\wood\sheep\sheep\sheep\sheep}{\sheep\sheep}

\end{document}

答案1

您的六边形稍宽,无法将其中 3 个放在一条线上。因此我引入了\mylength调整。您可以使用tabular列定义来排列图块:

\newcolumntype{C}{>{\centering\arraybackslash}p{\dimexpr0.32\textwidth-2\tabcolsep\relax}}

使用array包并像使用它们一样

\begin{tabular}{CCC}

代码:

\PassOptionsToPackage{draft}{graphicx}   %% uncomment in your system
\documentclass{article}
\usepackage[a4paper,margin=2mm]{geometry}
\usepackage{tikz,nopageno}
\usepackage{wasysym}
\usepackage{array}
\newcolumntype{C}{>{\centering\arraybackslash}p{\dimexpr0.32\textwidth-2\tabcolsep\relax}}

\usetikzlibrary{fit,backgrounds}

\setlength{\parskip}{0mm}
\newlength{\mywidth}
\setlength{\mywidth}{3cm}  %%% size of the hexagon adjusted so that three hexgons fit

\newcommand{\highlight}[2]{%
\begin{tikzpicture}[baseline = (text.base)]
  \node[inner sep=0pt] (text) {#2};
  \begin{pgfinterruptboundingbox}
    \begin{pgfonlayer}{background}
    \node[fit=(text), rounded corners, fill=white, opacity=#1, draw=none] {};
    \end{pgfonlayer}
  \end{pgfinterruptboundingbox}
\end{tikzpicture}%
}

% General macro to draw a shape filled with a bitmap
\newcommand\fillshape[3]{% #1 = shape, #2 = filename of texture, #3 = includegraphics options
    \begin{scope}
        \clip #1;
        \node {\includegraphics[#3]{#2}};
    \end{scope}
    \draw[line width=0.5mm] #1;
}%

% Particularization for hexagonal shape
\newcommand\fillhexagon[2][]{% #1 (optional) = includegraphics options, #2 = filename of texture
    \fillshape{(0:\mywidth) -- (60:\mywidth) -- (120:\mywidth) -- (180:\mywidth) -- (240:\mywidth) -- (300:\mywidth) -- cycle}{#2}{#1}
}

\newcommand\tile[6]{
\begin{tikzpicture}
    \fillhexagon[scale=#3]{#2};
    \node at (0,2.4) {\highlight{0.65}{\LARGE\bf #1}};
    \node at (0,0.7) {\highlight{1}{#6}};
    \draw [rounded corners, fill=white, opacity=1] (-2,-0.6)--(2,-0.6)--(2,-2.5)--(-2,-2.5)--cycle;
    \node at (0,-1.05) {#4};
    \node at (0,-1.85) {#5};
\end{tikzpicture}
}

\newcommand\image[2]{$\vcenter{\hbox{\includegraphics[scale=#1]{#2}}}$}

\newcommand\wood{\image{0.22}{wood.png}}
\newcommand\food{\image{0.23}{bowl.jpg}}
\newcommand\person{\image{0.25}{peasant.jpg}}
\newcommand\grain{\image{0.5}{grain2.jpg}}
\newcommand\sheep{\image{0.05}{sheep.png}}
\newcommand\stone{\image{0.35}{stone.png}}
\newcommand\ale{\image{0.12}{ale.png}}
%\newcommand\cloth{\image{0.2}{cloth.png}}
\newcommand\paper{\image{0.1}{paper.png}}
\newcommand\gold{\image{0.7}{gold.jpg}}
\newcommand\marble{\image{0.2}{marble.jpg}}

\newcommand\TO{\Huge$\rightarrow$}

\newcommand*{\vpointer}{$\vcenter{\hbox{\ \scalebox{2}{$\rightarrow$}}}$}

\newcommand\convert[2]{#1\vpointer\, #2}

\begin{document}
\begin{tabular}{CCC}
\tile{Lake}{lake.jpg}{1.2}{Starts on Board}{--}{\food}&
\tile{Field}{field2.jpg}{1.5}{Replaces Clearing}{\person\person\person\person}{\grain}&
\tile{Forest}{forest2.jpg}{0.4}{Starts on Board}{--}{\wood}\\%%[1ex] %% uncomment to adjust vertical gap

\tile{Mountain}{mountain2.jpg}{0.3}{Starts on Board}{--}{--}&
\tile{Baker}{baker.jpg}{7}{Build on Clearing}{\wood\wood\person}{\convert{\grain}{\food}}&
\tile{Butcher}{butcher.jpg}{10}{Build on Clearing}{\wood\wood\person}{\convert{\sheep}{\food}}\\%%[1ex] %% uncomment to adjust vertical gap

\tile{Grazing}{grazing.jpg}{0.3}{Replaces Hill}{\person}{\sheep}&
\tile{Pasture}{sheepfield.jpg}{2.1}{Replaces Clearing}{\wood\sheep\sheep\sheep\sheep}{\sheep\sheep}&
\end{tabular}

\end{document}

在此处输入图片描述

您可以调整行之间的垂直间隙以满足您的需要。抱歉图片不好,但我没有耐心为所有这些图片寻找替代品。

平铺版本tabular

\PassOptionsToPackage{draft}{graphicx}
\documentclass[a4paper]{article}
\usepackage[margin=2mm]{geometry}
\usepackage{tikz,nopageno}
\usepackage{wasysym}
\usepackage{array}
\newcolumntype{C}{>{\centering\arraybackslash}p{\dimexpr0.23\textwidth-2\tabcolsep\relax}}

\usetikzlibrary{fit,backgrounds}

\setlength{\parskip}{0mm}
\newlength{\mywidth}
\setlength{\mywidth}{3.7cm}  %%% size of the hexagon adjusted so that three hexgons fit

\newcommand{\highlight}[2]{%
\begin{tikzpicture}[baseline = (text.base)]
  \node[inner sep=0pt] (text) {#2};
  \begin{pgfinterruptboundingbox}
    \begin{pgfonlayer}{background}
    \node[fit=(text), rounded corners, fill=white, opacity=#1, draw=none] {};
    \end{pgfonlayer}
  \end{pgfinterruptboundingbox}
\end{tikzpicture}%
}

% General macro to draw a shape filled with a bitmap
\newcommand\fillshape[3]{% #1 = shape, #2 = filename of texture, #3 = includegraphics options
    \begin{scope}
        \clip #1;
        \node {\includegraphics[#3]{#2}};
    \end{scope}
    \draw[line width=0.5mm] #1;
}%

% Particularization for hexagonal shape
\newcommand\fillhexagon[2][]{% #1 (optional) = includegraphics options, #2 = filename of texture
    \fillshape{(0:\mywidth) -- (60:\mywidth) -- (120:\mywidth) -- (180:\mywidth) -- (240:\mywidth) -- (300:\mywidth) -- cycle}{#2}{#1}
}

\newcommand\tile[6]{
\begin{tikzpicture}
    \fillhexagon[scale=#3]{#2};
    \node at (0,2.4) {\highlight{0.65}{\LARGE\bf #1}};
    \node at (0,0.7) {\highlight{1}{#6}};
    \draw [rounded corners, fill=white, opacity=1] (-2,-0.6)--(2,-0.6)--(2,-2.5)--(-2,-2.5)--cycle;
    \node at (0,-1.05) {#4};
    \node at (0,-1.85) {#5};
\end{tikzpicture}
}

\newcommand\image[2]{$\vcenter{\hbox{\includegraphics[scale=#1]{#2}}}$}

\newcommand\wood{\image{0.22}{wood.png}}
\newcommand\food{\image{0.23}{bowl.jpg}}
\newcommand\person{\image{0.25}{peasant.jpg}}
\newcommand\grain{\image{0.5}{grain2.jpg}}
\newcommand\sheep{\image{0.05}{sheep.png}}
\newcommand\stone{\image{0.35}{stone.png}}
\newcommand\ale{\image{0.12}{ale.png}}
%\newcommand\cloth{\image{0.2}{cloth.png}}
\newcommand\paper{\image{0.1}{paper.png}}
\newcommand\gold{\image{0.7}{gold.jpg}}
\newcommand\marble{\image{0.2}{marble.jpg}}

\newcommand\TO{\Huge$\rightarrow$}

\newcommand*{\vpointer}{$\vcenter{\hbox{\ \scalebox{2}{$\rightarrow$}}}$}

\newcommand\convert[2]{#1\vpointer\, #2}

\begin{document}

\begin{tabular}{cCc}
\tile{Lake}{lake.jpg}{1.2}{Starts on Board}{--}{\food}&
&
\tile{Forest}{forest2.jpg}{0.4}{Starts on Board}{--}{\wood}\\[-3cm] %% uncomment to adjust vertical gap

&
\makebox[0pt][c]{\tile{Baker}{baker.jpg}{7}{Build on Clearing}{\wood\wood\person}{\convert{\grain}{\food}}}&
\\[-3cm] %% uncomment to adjust vertical gap

\tile{Lake}{lake.jpg}{1.2}{Starts on Board}{--}{\food}&
&
\tile{Forest}{forest2.jpg}{0.4}{Starts on Board}{--}{\wood}\\[-3cm] %% uncomment to adjust vertical gap

&
\makebox[0pt][c]{\tile{Baker}{baker.jpg}{7}{Build on Clearing}{\wood\wood\person}{\convert{\grain}{\food}}}&
\\[-3cm] %% uncomment to adjust vertical gap

\tile{Lake}{lake.jpg}{1.2}{Starts on Board}{--}{\food}&
&
\tile{Forest}{forest2.jpg}{0.4}{Starts on Board}{--}{\wood}\\[-3cm] %% uncomment to adjust vertical gap

&
\makebox[0pt][c]{\tile{Baker}{baker.jpg}{7}{Build on Clearing}{\wood\wood\person}{\convert{\grain}{\food}}}&
\\[-3cm] %% uncomment to adjust vertical gap

\tile{Lake}{lake.jpg}{1.2}{Starts on Board}{--}{\food}&
&
\tile{Forest}{forest2.jpg}{0.4}{Starts on Board}{--}{\wood}\\[-3cm] %% uncomment to adjust vertical gap

%&
%\makebox[0pt][c]{\tile{Baker}{baker.jpg}{7}{Build on Clearing}{\wood\wood\person}{\convert{\grain}{\food}}}&
%\\[-3cm] %% uncomment to adjust vertical gap

\end{tabular}
\end{document}

在此处输入图片描述

这可能会使切割变得困难,但我希望添加/删除行应该不会很困难。

答案2

如何定义一个坐标系?然后,您可以使用您选择的任何 tikz 方法来创建图块,然后只需使用坐标系移动变换矩阵即可。

对于图块内容略显枯燥,我深表歉意。若要将图块间隔得更开,只需将x和和y向量设置为更大的值即可。

\documentclass[tikz,border=0.125cm]{standalone}
\usepackage{tikz}
\usetikzlibrary{shapes.geometric}
\tikzset{hexagon/.style args={#1with image#2[#3]#4(#5)}{%
  regular polygon, regular polygon sides=6, 
  path picture={\node{\includegraphics[#3]{#5}};},
  draw, ultra thick, minimum size=#1, node contents=},
  %
  hexagonal cs/.cd,
    x/.store in=\hexx, y/.store in=\hexy,
    x=0,y=0, 
}
\tikzdeclarecoordinatesystem{hexagonal}{%
  \tikzset{hexagonal cs/.cd,#1}%
  \pgfpointxy{\hexx*0.75}{(\hexy-mod(\hexx,2)/2)*cos(30)}%
}
\begin{document}
\begin{tikzpicture}
\foreach \h [evaluate={\i=mod(\h,5); \j=floor(\h/5);
  \k=random(0,2); \l={"a","b","c"}[\k];}] in {0,...,24}
  \node [x=2.25cm, y=2.25cm] at (hexagonal cs:x=\j, y=\i) 
    [hexagon={2cm with image [scale=0.25] (example-image-\l)}];
\end{tikzpicture}
\end{document}

在此处输入图片描述

相关内容