将多个逗号分隔的 TikZ 选项传递给单个 keyval 键

将多个逗号分隔的 TikZ 选项传递给单个 keyval 键

我仍在改进我的回答需要帮助从 TikZ 中的一组 2D 节点创建 3D 立方体其中一个选项应该是强调长方体的边缘。这意味着例如

\draw[red, very thick, dashed] <some coordinates>

我用它keyval来定义长方体的参数。现在我的问题是,如何将所有这些命令传递给一个键?我会这样做:

\tikzcuboid{%
        emphedge=Y,%
        emphstyle=very thick,
    }

我只能传递一个选项(此处very thick),因为其他逗号(例如very thick, red, dashed)被解释为键的分隔符keyval。我尝试了几种方法,例如

emphstyle={very thick, red, dashed}
emphstyle=style={very thick, red, dashed}
emphstyle=(very thick, red, dashed)

所以我的问题基本上是:是否有某种特殊的分隔符,keyval我可以将逗号分隔的参数列表放入其中?但是,这些特殊分隔符不应传递,因为这可能会导致出现问题TikZ

答案1

为了保护代码中的逗号,你可以这样做

\ifthenelse{\equal{\tikzcuboid@emphedge}{Y}}%
    {\protected@edef\cube@temp{%
\noexpand\draw[\tikzcuboid@emphstyle](0,\dimy,0) -- (\dimx,\dimy,0) -- (\dimx,\dimy,\dimz) -- (0,\dimy,\dimz) -- cycle;     
\noexpand\draw[\tikzcuboid@emphstyle] (0,\dimy,\dimz) -- (0,0,\dimz) -- (\dimx,0,\dimz) -- (\dimx,\dimy,\dimz);
\noexpand\draw[\tikzcuboid@emphstyle](\dimx,\dimy,0) -- (\dimx,0,0) -- (\dimx,0,\dimz);
      }\cube@temp   
    }%
    {}

结果

\tikzcuboid{%
    shiftx=16cm,%
    shifty=8cm,%
    scale=1.00,%
    rotation=0,%
    densityx=2,%
    densityy=2,%
    densityz=2,%
    dimx=4,%
    dimy=4,%
    dimy=4,%
    linefront=green!75!black,%
    linetop=green!50!black,%
    lineright=green!25!black,%
    fillfront=green!25!white,%
    filltop=green!50!white,%
    fillright=green!75!white,%
    emphedge=Y,%
    emphstyle={very thick, red, dashed}
} 

和图片

在此处输入图片描述

答案2

如果您愿意改用,\pgfkeys那么这很简单。可以将一个键声明为键列表的别名。这可以通过以下方式完成:

\tikzset{my key/.style={list,of,other,keys}}

然后密钥以适当的方式my key扩展为。list,of,other,keys

以下是基于链接问题中的代码的操作示例。我在键名中添加了一些空格以使其更容易使用(这意味着我看到了一些错误:您曾多次dimy将 替换为)。我用布尔键替换了 。我还将和键压缩为一种样式,以提供更大的灵活性。您可以用简单的替换以获得最大的灵活性,然后可以随意填充或不填充网格。dimz\ifthenelselinefill\filldraw\path

代码如下:

\documentclass[parskip]{scrartcl}
\usepackage[margin=15mm,landscape]{geometry}
\usepackage{tikz}
%====================================
%emphasize vertices --> switch and emph style (e.g. thick,black)
%====================================

\newif\ifcuboidshaded
\newif\ifcuboidemphedge

\tikzset{
  cuboid/.is family,
  cuboid,
  shift x/.initial=0,
  shift y/.initial=0,
  dim x/.initial=3,
  dim y/.initial=3,
  dim z/.initial=3,
  scale/.initial=1,
  density x/.initial=1,
  density y/.initial=1,
  density z/.initial=1,
  rotation/.initial=0,
  angle x/.initial=0,
  angle y/.initial=90,
  angle z/.initial=225,
  scale x/.initial=1,
  scale y/.initial=1,
  scale z/.initial=sqrt(0.5),
  front/.style={draw=black,fill=white},
  top/.style={draw=black,fill=white},
  right/.style={draw=black,fill=white},
  shaded/.is if=cuboidshaded,
  shade color/.initial=black,
  shade perc/.initial=25,
  emph edge/.is if=cuboidemphedge,
  emph style/.style={thick},
}

\newcommand{\tikzcuboidkey}[1]{\pgfkeysvalueof{/tikz/cuboid/#1}}

% Commands
\newcommand{\tikzcuboid}[1]{
    \tikzset{cuboid,#1} % Process Keys passed to command
    \pgfmathsetmacro{\vectorxx}{\tikzcuboidkey{scale x}*cos(\tikzcuboidkey{angle x})}
    \pgfmathsetmacro{\vectorxy}{\tikzcuboidkey{scale x}*sin(\tikzcuboidkey{angle x})}
    \pgfmathsetmacro{\vectoryx}{\tikzcuboidkey{scale y}*cos(\tikzcuboidkey{angle y})}
    \pgfmathsetmacro{\vectoryy}{\tikzcuboidkey{scale y}*sin(\tikzcuboidkey{angle y})}
    \pgfmathsetmacro{\vectorzx}{\tikzcuboidkey{scale z}*cos(\tikzcuboidkey{angle z})}
    \pgfmathsetmacro{\vectorzy}{\tikzcuboidkey{scale z}*sin(\tikzcuboidkey{angle z})}
    \begin{scope}[xshift=\tikzcuboidkey{shift x}, yshift=\tikzcuboidkey{shift y}, scale=\tikzcuboidkey{scale}, rotate=\tikzcuboidkey{rotation}, x={(\vectorxx,\vectorxy)}, y={(\vectoryx,\vectoryy)}, z={(\vectorzx,\vectorzy)}]
    \pgfmathsetmacro{\steppingx}{1/\tikzcuboidkey{density x}}
    \pgfmathsetmacro{\steppingy}{1/\tikzcuboidkey{density y}}
    \pgfmathsetmacro{\steppingz}{1/\tikzcuboidkey{density z}}
    \newcommand{\dimx}{\tikzcuboidkey{dim x}}
    \newcommand{\dimy}{\tikzcuboidkey{dim y}}
    \newcommand{\dimz}{\tikzcuboidkey{dim z}}
    \pgfmathsetmacro{\secondx}{2*\steppingx}
    \pgfmathsetmacro{\secondy}{2*\steppingy}
    \pgfmathsetmacro{\secondz}{2*\steppingz}
    \foreach \x in {\steppingx,\secondx,...,\dimx}
    {   \foreach \y in {\steppingy,\secondy,...,\dimy}
        {   \pgfmathsetmacro{\lowx}{(\x-\steppingx)}
            \pgfmathsetmacro{\lowy}{(\y-\steppingy)}
            \filldraw[cuboid/front] (\lowx,\lowy,\dimz) -- (\lowx,\y,\dimz) -- (\x,\y,\dimz) -- (\x,\lowy,\dimz) -- cycle;

        }
    }
    \foreach \x in {\steppingx,\secondx,...,\dimx}
    {   \foreach \z in {\steppingz,\secondz,...,\dimz}
        {   \pgfmathsetmacro{\lowx}{(\x-\steppingx)}
            \pgfmathsetmacro{\lowz}{(\z-\steppingz)}
            \filldraw[cuboid/top] (\lowx,\dimy,\lowz) -- (\lowx,\dimy,\z) -- (\x,\dimy,\z) -- (\x,\dimy,\lowz) -- cycle;
        }
    }
    \foreach \y in {\steppingy,\secondy,...,\dimy}
    {   \foreach \z in {\steppingz,\secondz,...,\dimz}
        {   \pgfmathsetmacro{\lowy}{(\y-\steppingy)}
            \pgfmathsetmacro{\lowz}{(\z-\steppingz)}
            \filldraw[cuboid/right] (\dimx,\lowy,\lowz) -- (\dimx,\lowy,\z) -- (\dimx,\y,\z) -- (\dimx,\y,\lowz) -- cycle;
        }
    }
    \ifcuboidemphedge
        \draw[cuboid/emph style] (0,\dimy,0) -- (\dimx,\dimy,0) -- (\dimx,\dimy,\dimz) -- (0,\dimy,\dimz) -- cycle;%
        \draw[cuboid/emph style] (0,\dimy,\dimz) -- (0,0,\dimz) -- (\dimx,0,\dimz) -- (\dimx,\dimy,\dimz);%
        \draw[cuboid/emph style] (\dimx,\dimy,0) -- (\dimx,0,0) -- (\dimx,0,\dimz);%
    \fi
    \end{scope}
}

\makeatother

\begin{document}

\begin{tikzpicture}
    \tikzcuboid{shift x=0cm,%
        shift y=0cm,%
        scale=1.00,%
        rotation=30,%
        density x=1,%
        density y=2,%
        density z=3,%
        dim x=4,%
        dim y=5,%
        dim z=2,%
        front/.style={draw=yellow!30!black,fill=yellow!30!white},%
        top/.style={draw=red!30!black,fill=red!30!white},%
        right/.style={draw=blue!30!black,fill=blue!30!white}%
    }
    \tikzcuboid{%
        shift x=0cm,%
        shift y=8cm,%
        scale=1.00,%
        rotation=60,%
        density x=3,%
        density y=2,%
        density z=5,%
        dim x=4,%
        dim y=4,%
        dim z=4,%
        front/.style={draw=orange!75!black,fill=orange!75!white},%
        top/.style={draw=green!75!black,fill=green!75!white},%
        right/.style={draw=violet!75!black,fill=violet!75!white}%
    }
    \tikzcuboid{%
        shift x=8cm,%
        shift y=8cm,%
        scale=1.00,%
        rotation=45,%
        density x=1,%
        density y=(2/3),%
        density z=2,%
        dim x=3,%
        dim y=3,%
        dim y=3,%
        front/.style={draw=white!15!black,fill=black!15!white},%
        top/.style={draw=white!30!black,fill=black!30!white},%
        right/.style={draw=white!45!black,fill=black!45!white}%
    }
    \tikzcuboid{%
        shift x=8cm,%
        shift y=0cm,%
        scale=1.00,%
        rotation=75,%
        density x=2,%
        density y=3,%
        density z=2,%
        dim x=6,%
        dim y=8,%
        dim z=1,%
        front/.style={draw=red!75!black,fill=red!25!white},%
        top/.style={draw=red!50!black,fill=red!50!white},%
        right/.style={draw=red!25!black,fill=red!75!white}%
    }
    \tikzcuboid{%
        shift x=16cm,%
        shift y=8cm,%
        scale=1.00,%
        rotation=0,%
        density x=2,%
        density y=2,%
        density z=2,%
        dim x=4,%
        dim y=4,%
        dim y=4,%
        front/.style={draw=green!75!black,fill=green!25!white},%
        top/.style={draw=green!50!black,fill=green!50!white},%
        right/.style={draw=green!25!black,fill=green!75!white},%
        emph edge,%
        emph style/.style={very thick},
    }
    \tikzcuboid{%
        shift x=16cm,%
        shift y=0cm,%
        scale=1.00,%
        rotation=0,%
        density x=1,%
        density y=1,%
        density z=1,%
        dim x=4,%
        dim y=4,%
        dim z=4,%
        front/.style={draw=blue!75!black,fill=blue!25!white},%
        right/.style={draw=blue!25!black,fill=blue!75!white},%
        top/.style={draw=blue!50!black,fill=blue!50!white},%
        angle x=15,%
        angle y=135,%
        angle z=225,%
        scale x=1,%
        scale y=1,%
        scale z=1,%
        emph edge=false,%
    }
\end{tikzpicture}

\end{document}

相关内容