我有一些 TikZ 代码,用于绘制由小圆圈图像组成的圆圈(用于显示一组人的照片)。显示的人数可能会有所不同,因此我尝试删除硬编码的数字,以便更轻松地针对不同的人数更改代码。
我已对此进行了初步尝试,定义了变量numberOfMembers
和startAngle
。变量numberOfMembers
用于计算以划分圆。
我怎样才能减少此代码中的硬编码?
例如,我如何制作图像列表,而不是用索引列出每个图像(在此示例中,最多列出图像 8)?如何进行除法运算以产生实数输出?
\documentclass[american, a4paper]{report}
\usepackage{graphicx}
\usepackage{color}
\usepackage{tikz}
\definecolor{CERNBlue1}{RGB}{0, 83, 161}
\def\basiceval#1{\the\numexpr#1\relax}
\begin{document}
\newcommand{\imagesDirectory}{images}
\def\numberOfMembers{8}
\def\startAngle{30}
\tikzset{%
image 1/.initial=\imagesDirectory/AK.png,
image 2/.initial=\imagesDirectory/SB.png,
image 3/.initial=\imagesDirectory/WBM.png,
image 4/.initial=\imagesDirectory/MP.png,
image 5/.initial=\imagesDirectory/SN.png,
image 6/.initial=\imagesDirectory/RETT.png,
image 7/.initial=\imagesDirectory/GA.png,
image 8/.initial=\imagesDirectory/JR.png,
path image/.style={path picture={%
\edef\imageFile{\pgfkeysvalueof{/tikz/image #1}}%
\node at (path picture bounding box.center){
\includegraphics[height=1 cm]{\imageFile}
};
}}
}
\begin{center}
\resizebox{10 cm}{!}{%
\begin{tikzpicture}
\foreach \i in {1,...,\numberOfMembers}
\draw [
path image=\i,
color=CERNBlue1
](
\i * \basiceval{360/\numberOfMembers} + \startAngle:1.5
)
circle [radius=0.5 cm];
\end{tikzpicture}
}
\end{center}
\end{document}
答案1
这是您可以进一步修改的另一种方法
\documentclass[american, a4paper]{report}
%\usepackage[demo]{graphicx} %<-- use demo to hide errors
\usepackage{mwe}
\usepackage{tikz}
% Sample image name array
\def\imagenamearray{{"example-image-a","example-image-b","example-image","example-image-c",
"example-image-16x10","example-image-10x16","example-image-golden","example-image-16x9"}}
\tikzset{%
team necklace start angle/.initial=0,
team necklace start angle=0,
team necklace options/.style={draw,circle,inner sep=0.5cm},
team necklace draw/.code={%
\pgfmathtruncatemacro\numberOfMembers{dim(#1)-1}%
\pgfmathsetmacro\teamnecklaceanglestep{(360/\numberOfMembers)}%
\foreach\x in{0,...,\numberOfMembers}{%
\node[team necklace options,path picture={%
\node at (path picture bounding box.center){%
\pgfmathparse{#1[\x]}%
\includegraphics[height=1 cm]{\pgfmathresult}%
};%
}] at (\x*\teamnecklaceanglestep+\pgfkeysvalueof{/tikz/team necklace start angle}:2cm) {};
}
}
}
\begin{document}
\begin{tikzpicture}
\path[team necklace start angle=30,team necklace draw=\imagenamearray];
\end{tikzpicture}
\end{document}
答案2
这样做相当复杂。我假设你无论如何都要给出每个文件名,所以我会将它们包含在 for each 中,并使用第二个变量。
这是缩小的可编译 MWE,带有示例图像,您应该使用一个(用于将来的帖子:)
更新:现在你只需要将文件名列表传递给 foreach
\documentclass{standalone}
\usepackage{graphicx,color,tikz}
\definecolor{CERNBlue1}{RGB}{0, 83, 161}
\def\basiceval#1{\the\numexpr#1\relax}
\def\numberOfMembers{4}
\def\startAngle{30}
\begin{document}
\begin{tikzpicture}
\foreach \v [count=\i from 0] in {
example-image-a,
example-image-b,
example-image-a,
example-image-b}
\draw [
path picture={
\node at (path picture bounding box.center){
\includegraphics[height=1 cm]{\v}
};}
color=CERNBlue1
](
\i * \basiceval{360/\numberOfMembers} + \startAngle:1.5
)
circle [radius=0.5 cm];
\end{tikzpicture}
\end{document}