10

How can I use a variable beyond the environment where it has been set?

Following the MWE given below, how can I print the variable it both places (inside and outside the environment) ?

\documentclass{article}
\usepackage[utf8]{inputenc}

\newcommand\referenceSente{}
\newcommand{\defReferenceSente}[1]{\renewcommand{\referenceSente}{#1}} 
\newcommand{\printReferenceSente}{\referenceSente}
\newenvironment{myEnvironment}[1]{\defReferenceSente{#1}}

\begin{document}
\begin{myEnvironment}{keyword}
Inside environment : \printReferenceSente
\end{myEnvironment}
Outside environment : \printReferenceSente

\end{document}
  • First of all, you need to use the right syntax for \newcommand and \newenvironment. Read what you find at the end of those two links and modify your MWE accordingly; then we can look at the scoping problem. – jub0bs Jun 26 '14 at 9:41
  • Right, sorry, wrong version of the WME. It should work now. – BonyHoax Jun 26 '14 at 9:52
  • Your \newenvironment is missing its last argument. Have another look at the syntax of that command. – jub0bs Jun 26 '14 at 10:02
14
+150

LaTeX2e doesn't provide a command for defining a macro at the global scope; you have to resort to something like TeX's \gdef (read as "global definition") for that.

Besides, your \printReferenceSente is superfluous, here.

The code below should do what you want.

enter image description here

\documentclass{article}

% initialise macro
\newcommand\referenceSente{}

% command for defining \referenceSente macro globally
\newcommand{\gdefReferenceSente}[1]
  {\gdef\referenceSente{#1}}


\newenvironment{myEnvironment}[1]
  {\gdefReferenceSente{#1}} % before
  {\par}                    % after

\begin{document}
\begin{myEnvironment}{keyword}
Inside environment : \referenceSente
\end{myEnvironment}
Outside environment : \referenceSente

\end{document}
9

a version closer to your code, using \gdef (as Jubobs rightly identifies as necessary) only where it really matters -- to "trap" your keyword, and with his simplification without a special print command ...

\documentclass{article}
\usepackage[utf8]{inputenc}

\newcommand\referenceSente{}
\newcommand{\defReferenceSente}[1]{\gdef\referenceSente{#1}} 
\newenvironment{myEnvironment}[1]{\defReferenceSente{#1}}{}

\begin{document}
\begin{myEnvironment}{keyword}
Inside environment : \referenceSente
\end{myEnvironment}
Outside environment : \referenceSente

\end{document}

edit: the original example has only one (the beginning) component to the definition of \newenvironment{myEnvironment}; it was "saved" by the presence of a blank line -- a "silent \par" -- between the definition and \begin{document}, because the \par is interpreted as the second (required) component of the environment definition. if the blank line is removed, an error results:

! LaTeX Error: Missing \begin{document}.

See the LaTeX manual or LaTeX Companion for explanation.
Type  H <return>  for immediate help.
 ...                                              

l.7 \begin{d
            ocument}
?

to fix that problem is easy -- just add an empty group, {} as the ending component, to do nothing but make the definition syntactically correct. (thanks to Jubobs for catching this glitch.)

  • The last argument of \newenvironment is an "invisible" \par, here. Surely, that's because the OP is not familiar with the syntax. Isn't it dangerous to leave it like that? – jub0bs Jun 26 '14 at 14:07
  • I saw that {\par} in the first solution proposed, but as you said, I have no idea why I should add this. What is the use of this line? – BonyHoax Jun 26 '14 at 14:39
  • @Jubobs -- as you note, environments do incorporate an "invisible" \par at the end. so whether or not it's dangerous depends on how it's going to be applied. if no \par is wanted, then an environment wouldn't be appropriate. but presumably since it's the environment that's localizing the definition, an environment is wanted there. the use of the globalized term doesn't include the \par, so it should be okay wherever it's used. – barbara beeton Jun 26 '14 at 16:48
  • 1
    @barbarabeeton Sorry, I didn't express myself clearly. What I find dangerous here is that the last mandatory argument of \newenvironment in the OP's code (and in yours) happens to be an implicit \par token. If the OP removes that token (by removing the blank line above \begin{document}, errors will arise. \newenvironment{myEnvironment}[1]{\defReferenceSente{#1}}{} would be safer. – jub0bs Jun 26 '14 at 18:12
  • 1
    @Jubobs -- aha! now i've got it! never saw this happen myself, but indeed it does -- an error Missing \begin{document}. i'll fix it in the example. thanks. – barbara beeton Jun 26 '14 at 19:00

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.