3

I'm trying to create a custom environment that takes care of positioning certain pieces of information at certain coordinates within a minipage. The positioning works fine, however I try to provide the information through commands bound to the environment to be able to separate the kinds of information, which looks something like this:

\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}
}

A sample use of the environment:

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

As indicated by the comments in the code above, this will print the argument just fine. The content will appear too; however, instead of "new value", "original value" is printed.

If I simplify the environment a lot, the value will, however, be "new value", as intended:

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

This leaves me clueless as to what I may be doing wrong.

I'm happy to provide any more details as requested.

Update

Right now, it looks like the issue may be caused by grouping that's broken once a minipage is added that wraps the main content of the environment, like so:

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

Using \gdef instead of \renewcommand did not change anything for me:

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

As per Ulrike Fischer's suggestion, here's a complete example using \gdef that doesn't work for me:

\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}

Update 2(.5)

Code example for the original problem with the usage of the command moved to the very end of the environment:

\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}
  • Off-topic: don't use \\ to break lines. – cfr Jun 7 '17 at 22:38
  • Let us continue this discussion in chat. – cfr Jun 7 '17 at 22:55
  • Yes, I meant \newcommand{\chsskills}{\gdef\givenchsskills{#1}}, it was a typo, and I forgot to mention to define it outside of the chs environment – user31729 Jun 8 '17 at 9:04
  • Without the latter part, it didn't work indeed. @cfr pointed this out to me, so now it works! I'm not sure how to go about answering the question now though (as in, answer so it's marked "answered"). – koala Jun 8 '17 at 9:19
  • @koala: I think you should redesign the whole environment anyway and leave this 'unanswered'. – user31729 Jun 8 '17 at 9:57
1

You need the \gdef trick, in addition to putting \newcommand outside the environment. So I have

\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
}

Then it works without error and I get 'new value' and 'newest value' respectively at the end of the environments.

But note that \givenchsskills will give the current value outside the environment, too. If you don't want that, you can \gdef it to original value or \relax at the very end of the environment.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.