将矩阵/tikzpicture 打包成一个新命令,同时保持样式内的参数

将矩阵/tikzpicture 打包成一个新命令,同时保持样式内的参数

我有一个 SWOT 矩阵,它在文本主体中使用时效果很好。

在此处输入图片描述

我想打包它并使用类似的东西:

  \swotfigure{foo}{bar}{frut}{boo}

即带有 4 个参数的新命令。

它使用带有 tkizpicture 的矩阵;其中填充颜色也是参数。

\begin{tikzpicture}[
    any/.style={
......
    mycolor/.style={fill=#1, text=#1!0!white}

但是,当将其包装在新命令中时;因此可以将其移动到样式中,人们会遇到两层参数的问题。我试图通过将其移动到序言中来避免这种情况。但下面的代码给了我一个错误(定义中的非法参数,实际上可以忽略它仍然可以获得正确的输出)。

\documentclass[11pt,a4,final,abstracton]{book}
    
\usepackage{array}
\newcolumntype{P}[1]{>{\raggedright\arraybackslash}p{#1}}

\usepackage{tikz}
\usepackage{pgf-umlsd}
\usetikzlibrary{matrix}

\colorlet{helpful}{lime!70}
\colorlet{harmful}{red!30}
\colorlet{internal}{yellow!20}
\colorlet{external}{cyan!30}
\colorlet{S}{helpful!50!internal}
\colorlet{W}{harmful!50!internal}
\colorlet{O}{helpful!50!external}
\colorlet{T}{harmful!50!external}

\newcommand{\texta}{Helpful\\ \tiny (to achieve the objective)\par}
\newcommand{\textb}{Harmful\\ \tiny (to achieve the objective)\par}
\newcommand{\textcn}{Internal origin\\ \tiny (product\slash company attributes)\par}
\newcommand{\textdn}{External origin\\ \tiny (environment\slash market attributes)\par}
\newcommand{\back}[1]{\fontsize{60}{70}\selectfont #1}

\newcommand{\swotfigurePreamble}{
\begin{tikzpicture}[
    any/.style={
        minimum width=\textwidth/3, % this looks OK - but not yet found away to keep aspect ration the same as page.
        minimum height=\textwidth/3,%
        text width=\textwidth/3-0.5cm,
        align=center,outer sep=0pt},
    header/.style={any,minimum height=1cm,fill=black!10},
    leftcol/.style={header,rotate=90},
    mycolor/.style={fill=#1, text=#1!0!white}
]
\matrix (SWOT) [matrix of nodes,nodes={any,anchor=center},%
                column sep=-\pgflinewidth,%
                row sep=-\pgflinewidth,%
                row 1/.style={nodes=header},%
                column 1/.style={nodes=leftcol},
                ampersand replacement=\&,
                inner sep=0pt]{
                            \& |[fill=helpful]| {\texta} \& |[fill=harmful]| {\textb} \\
|[fill=internal]| {\textcn} \& |[mycolor=S]| \back{S}    \& |[mycolor=W]| \back{W} \\
|[fill=external]| {\textdn} \& |[mycolor=O]| \back{O}    \& |[mycolor=T]| \back{T} \\
};
}

\newcommand{\swotfigure}[4]{
\begin{center}
\swotfigurePreamble
\node[any, anchor=center] at (SWOT-2-2) {#1};
\node[any, anchor=center] at (SWOT-2-3) {#2};
\node[any, anchor=center] at (SWOT-3-2) {#3};
\node[any, anchor=center] at (SWOT-3-3) {#4};
\end{tikzpicture}
\end{center}
}

\begin{document}

\begin{figure}[htbp]
\swotfigure{foo}{bar}{frut}{boo}

\end{figure}
\end{document}

正确的做法是什么?

答案1

对于单数#,TeX 期望其后跟一个数字,并假设它指的是一个参数。为了避免这种解释,双数,则在下一次迭代处理中将#其视为单数。#

    mycolor/.style={fill=##1, text=##1!0!white}

此外,要小心行尾处引入的虚假空格,例如

\newcommand{\swotfigurePreamble}{
\newcommand{\swotfigure}[4]{

最好通过添加来删除行尾和相应的空格%

\newcommand{\swotfigurePreamble}{%
\newcommand{\swotfigure}[4]{%

在此处输入图片描述

\documentclass[11pt,a4,final,abstracton]{book}
    
\usepackage{array}
\newcolumntype{P}[1]{>{\raggedright\arraybackslash}p{#1}}

\usepackage{tikz}
\usepackage{pgf-umlsd}
\usetikzlibrary{matrix}

\colorlet{helpful}{lime!70}
\colorlet{harmful}{red!30}
\colorlet{internal}{yellow!20}
\colorlet{external}{cyan!30}
\colorlet{S}{helpful!50!internal}
\colorlet{W}{harmful!50!internal}
\colorlet{O}{helpful!50!external}
\colorlet{T}{harmful!50!external}

\newcommand{\texta}{Helpful\\ \tiny (to achieve the objective)\par}
\newcommand{\textb}{Harmful\\ \tiny (to achieve the objective)\par}
\newcommand{\textcn}{Internal origin\\ \tiny (product\slash company attributes)\par}
\newcommand{\textdn}{External origin\\ \tiny (environment\slash market attributes)\par}
\newcommand{\back}[1]{\fontsize{60}{70}\selectfont #1}

\newcommand{\swotfigurePreamble}{% <<<<<<<
\begin{tikzpicture}[
    any/.style={
        minimum width=\textwidth/3, % this looks OK - but not yet found away to keep aspect ration the same as page.
        minimum height=\textwidth/3,%
        text width=\textwidth/3-0.5cm,
        align=center,outer sep=0pt},
    header/.style={any,minimum height=1cm,fill=black!10},
    leftcol/.style={header,rotate=90},
    mycolor/.style={fill=##1, text=##1!0!white}
]
\matrix (SWOT) [matrix of nodes,nodes={any,anchor=center},%
                column sep=-\pgflinewidth,%
                row sep=-\pgflinewidth,%
                row 1/.style={nodes=header},%
                column 1/.style={nodes=leftcol},
                ampersand replacement=\&,
                inner sep=0pt]{
                            \& |[fill=helpful]| {\texta} \& |[fill=harmful]| {\textb} \\
|[fill=internal]| {\textcn} \& |[mycolor=S]| \back{S}    \& |[mycolor=W]| \back{W} \\
|[fill=external]| {\textdn} \& |[mycolor=O]| \back{O}    \& |[mycolor=T]| \back{T} \\
};
}

\newcommand{\swotfigure}[4]{% <<<<<<
\begin{center}
\swotfigurePreamble
\node[any, anchor=center] at (SWOT-2-2) {#1};
\node[any, anchor=center] at (SWOT-2-3) {#2};
\node[any, anchor=center] at (SWOT-3-2) {#3};
\node[any, anchor=center] at (SWOT-3-3) {#4};
\end{tikzpicture}
\end{center}
}

\begin{document}

\begin{figure}[htbp]
\swotfigure{foo}{bar}{frut}{boo}

\end{figure}
\end{document}

相关内容