我正在尝试使用子字符串将三位数参数 (101) 存储在三个不同的变量中。根据输入,我想在矩形的右上角放置一到三个不同的图标。子字符串的内容看起来不错,但以下错误阻止了正确的输出。
\@xs@StrMid 的参数有一个额外的}。\end{info}
如果我使用三个参数它可以工作,但目标是只用一个参数来完成。
以下是我的示例:
\documentclass{scrartcl}
\usepackage{environ}
\usepackage{tikz}
\usepackage{xstring}
\definecolor{main-color-05}{HTML}{BF3100}
\definecolor{dark-color}{HTML}{2B2B2B}
\definecolor{light-color}{HTML}{F6F6F6}
\tikzset{
box/.style={rectangle, fill=light-color, inner sep=10pt},
border/.style={draw=main-color-05, line width=2pt},
}
% does not work
\NewEnviron{info}[1]{
\newcommand{\posone}{\StrMid{#1}{1}{1}}
\newcommand{\postwo}{\StrMid{#1}{2}{2}}
\newcommand{\posthree}{\StrMid{#1}{3}{3}}
\begin{figure}[h]
\centering
\begin{tikzpicture}
\node[box] (m) {%
\begin{minipage}{0.925\textwidth}
{\LARGE\bfseries\color{dark-color}Lorem Ipsum}\hfill \posone\postwo\posthree
\ifnum\pdfstrcmp{\posone}{1}=0
\,first-icon
\fi
\ifnum\pdfstrcmp{\postwo}{1}=0
\,second-icon
\fi
\ifnum\pdfstrcmp{\posthree}{1}=0
\,third-icon
\fi\\
\BODY
\end{minipage}
};
\draw[border] (m.south west) -- (m.north west);
\end{tikzpicture}
\end{figure}
}
% work
\NewEnviron{info1}[3]{
\begin{figure}[h]
\centering
\begin{tikzpicture}
\node[box] (m) {%
\begin{minipage}{0.925\textwidth}
{\LARGE\bfseries\color{dark-color}Lorem Ipsum}\hfill
\ifnum\pdfstrcmp{#1}{1}=0
\,Icon1
\fi
\ifnum\pdfstrcmp{#2}{1}=0
\,Icon2
\fi
\ifnum\pdfstrcmp{#3}{1}=0
\,Icon3
\fi
\\
\BODY
\end{minipage}
};
\draw[border] (m.south west) -- (m.north west);
\end{tikzpicture}
\end{figure}
}
\begin{document}
% does not work
\begin{info}{321}
Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet. Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet.
\end{info}
% work
\begin{info1}{1}{0}{1}
Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet. Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet.
\end{info1}
\end{document}
我希望有人能帮助我。抱歉我的英语不好。
制造商寻求帮助。
答案1
这是对工作原理的常见误解xstring
;代码\StrMid{#1}{1}{1}
是不是字符串中的第一个项目,但获取它的说明。该包提供了一个尾随可选参数来存储结果:
\documentclass{scrartcl}
\usepackage{environ}
\usepackage{tikz}
\usepackage{xstring}
\definecolor{main-color-05}{HTML}{BF3100}
\definecolor{dark-color}{HTML}{2B2B2B}
\definecolor{light-color}{HTML}{F6F6F6}
\tikzset{
box/.style={rectangle, fill=light-color, inner sep=10pt},
border/.style={draw=main-color-05, line width=2pt},
}
% does not work
\NewEnviron{info}[1]{%
\StrMid{#1}{1}{1}[\posone]%
\StrMid{#1}{2}{2}[\postwo]%
\StrMid{#1}{3}{3}[\posthree]%
\begin{figure}[htp]
\centering
\begin{tikzpicture}
\node[box] (m) {%
\begin{minipage}{0.925\textwidth}
{\LARGE\bfseries\color{dark-color}Lorem Ipsum}\hfill\posone\postwo\posthree
\ifnum\pdfstrcmp{\posone}{1}=0
\,first-icon
\fi
\ifnum\pdfstrcmp{\postwo}{1}=0
\,second-icon
\fi
\ifnum\pdfstrcmp{\posthree}{1}=0
\,third-icon
\fi\\
\BODY
\end{minipage}
};
\draw[border] (m.south west) -- (m.north west);
\end{tikzpicture}
\end{figure}
}
\begin{document}
Works!
\begin{info}{321}
Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut
labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo duo dolores
et ea rebum. Stet clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet. Lorem
ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et
dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo duo dolores et ea
rebum. Stet clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet.
\end{info}
\end{document}
避免使用单一说明符[h]
,否则如果图形无法放置在该位置,您将陷入麻烦。
另一方面,你可以完全避免xstring
:添加
\newcommand{\extract}[1]{\extractaux#1...\extractaux}
\def\extractaux#1#2#3#4\extractaux{%
\def\posone{#1}%
\def\postwo{#2}%
\def\posthree{#3}%
}
并做
\NewEnviron{info}[1]{%
\extract{#1}%
\begin{figure}[htp]
...
}