调用自定义命令在自定义环境中设置值不起作用

调用自定义命令在自定义环境中设置值不起作用

我正在尝试创建一个自定义环境,用于将某些信息定位到小页面中的某些坐标。定位工作正常,但我尝试通过绑定到环境的命令提供信息,以便能够区分信息类型,如下所示:

\newenvironment{chs}[1]
{
% \givenchsskills functions as a variable of sorts; its initial value is "original value"
\newcommand{\givenchsskills}{original value}
% \chsskills is used to set the variable by renewing the command \givenchsskills and having it return the argument. 
% Double # because we're in an environment that also uses arguments.
\newcommand{\chsskills}[1]{\renewcommand{\givenchsskills}{##1}}

% Stuff concerning positioning
\setlength{\TPHorizModule}{1cm}
\setlength{\TPVertModule}{1cm}

% This adjustbox serves the sole purpose of providing a background image. The latter is the reason for the weird positioning values.
\begin{adjustbox}{minipage=[t][\textheight]{\textwidth}, bgimage=bg_sheet.png}

% Textblocks allow for positioning relative to the environments coordinates. \fboxsep0pt is used to prevent box separation in the combination of minipage and mbox...
\begin{textblock}{5}(2.15,.8)\fboxsep0pt
% ... which i'm using to prevent linebreaks and still be able to position the text. Probably messy; I'm far from being a clean LaTeX user... Anyway, this prints the argument for the environment.
\begin{minipage}[t][.57cm][c]{8.8cm}\mbox{\large #1}\end{minipage}
\end{textblock}

% Positioning of the main content for this environment which itself is wrapped inside a minipage the restricts it to a certain size box. Kind of.
\vspace{2.8cm}
\hspace{.8cm}
\begin{minipage}[t][6.6cm]{7.5cm}
}
{
\end{minipage}

% Another textblock for positioning
\begin{textblock}{5}(.8,2)\fboxsep0pt
% This is merely to help me with finetuning the actual position by eyeing it. I can still see the background and the colored bounding box that will contain the text.
\transparent{.5}\colorbox{green}{
\begin{minipage}[t][.4cm][c]{10.1cm}
% This is where we get to the problem! This will print "original value", no matter if \chsskills has been used or not.
\givenchsskills
\end{minipage}
}
\end{textblock}
\end{adjustbox}
}

环境使用示例:

\begin{chs}{Some argument}
    Test content
    \chsskills{new value}
\end{chs}

如上面代码中的注释所示,这将正确打印参数。内容也会出现;但是,打印的不是“新值”,而是“原始值”。

如果我大大简化环境,那么值将会是“新值”,正如预期的那样:

\newenvironment{chs}[1]
{
\newcommand{\givenchsskills}{original value}
\newcommand{\chsskills}[1]{\renewcommand{\givenchsskills}{##1}}
Argument: #1\\
}
{
\\
Skills: \givenchsskills
}

这让我完全不知道我可能做错了什么。

我很乐意根据要求提供更多详细信息。

更新

现在,问题似乎是由添加包装环境主要内容的小页面后分组中断而引起的,如下所示:

\newenvironment{chs}[1]
{
\newcommand{\givenchsskills}{original value}
\newcommand{\chsskills}[1]{\renewcommand{\givenchsskills}{##1}}
Argument: #1\\
Skills: \givenchsskills\\
\begin{minipage}{8cm}
}
{
\end{minipage}
}

使用\gdef而不是\renewcommand对我来说没有任何改变:

\newcommand{\chsskills}[1]{\gdef\givenchsskills{##1}}

按照Ulrike Fischer 的建议,这里有一个完整的例子,但\gdef它对我来说不起作用:

\documentclass[10pt,a4paper]{article}

\newenvironment{chs}[1]
{
\newcommand{\givenchsskills}{original value}
\newcommand{\chsskills}[1]{\gdef\givenchsskills{##1}}
Argument: #1\\
Skills: \givenchsskills\\
\begin{minipage}{8cm}
}
{
\end{minipage}
}

\begin{document}
\begin{chs}{Some argument}
    Test content
    \chsskills{new value}
\end{chs}
\end{document}

更新 2(.5)

将命令的使用移至环境最末端的原始问题的代码示例:

\documentclass[10pt,a4paper]{article}

\usepackage{graphicx}
\usepackage{color}
\usepackage{textpos}
\usepackage{adjustbox}
\usepackage{transparent}

\newcommand{\givenchsskills}{original value}
\newcommand{\chsskills}[1]{\renewcommand{\givenchsskills}{#1}}
\newenvironment{chs}[1]
{
\setlength{\TPHorizModule}{1cm}
\setlength{\TPVertModule}{1cm}
\begin{adjustbox}{minipage=[t][\textheight]{\textwidth}, bgimage=bg_sheet.png}
\begin{textblock}{5}(2.15,.8)\fboxsep0pt
\begin{minipage}[t][.57cm][c]{8.8cm}\mbox{\large #1}\end{minipage}
\end{textblock}
\vspace{2.8cm}
\hspace{.8cm}
\begin{minipage}[t][6.6cm]{7.5cm}
}
{
\end{minipage}
\end{adjustbox}
\givenchsskills
}

\begin{document}
\begin{chs}{Some argument}
    Test content
    \chsskills{new value}
\end{chs}

\begin{chs}{Some argument}
    Test content
    \chsskills{newest value}
\end{chs}
\end{document}

答案1

\gdef除了放在\newcommand环境之外,你还需要技巧。所以我有

\newcommand{\givenchsskills}{original value}
\newcommand{\chsskills}[1]{%
  \gdef\givenchsskills{#1}%
}
\newenvironment{chs}[1]
{%
  \setlength{\TPHorizModule}{1cm}%
  \setlength{\TPVertModule}{1cm}%
  \begin{adjustbox}{minipage=[t][\textheight]{\textwidth}, bgimage=example-image-a}%
    \begin{textblock}{5}(2.15,.8)\fboxsep0pt%
      \begin{minipage}[t][.57cm][c]{8.8cm}\mbox{\large #1}\end{minipage}%
    \end{textblock}%
    \vspace{2.8cm}%
    \hspace{.8cm}%
    \begin{minipage}[t][6.6cm]{7.5cm}%
    }{%
    \end{minipage}%
  \end{adjustbox}\par
  \givenchsskills
}

然后它可以无错误地运行,并且我在环境结束时分别获得“新值”和“最新值”。

但请注意,这\givenchsskills也会将当前值提供给环境之外。如果您不想这样,您可以将\gdef其设置为原始值或\relax环境的最末尾。

相关内容