1

Edit Preface: Surprised this wasn't downvoted into oblivion as it was a really terribly written question... this is what I get for posting without thinking at 2am after a long day of coding... sorry guys.

I have determined the nature of the error, although why the blank environment helped I have no idea, so I have completely rewritten the question so it's clear including a useful MWE.

Question:

It seems that a \begin environment resets logic checks to false?

Observe the code below:

\documentclass[]{article}

\newif\iffooLogic
\fooLogicfalse

\newcommand{\fooCommand}{
%Don't include \fooLogicfalse here to make the problem
\iffooLogic
    {The First Is True!}
\else
    {The First is False!}
\fi

%Set \fooLogic to true for next line
\fooLogictrue

\iffooLogic
    {The Second is True!}
\else
    {The Second is False!}
\fi
% No reset of \fooLogic to false to create problem.
}

\newenvironment{testOne}{•}{•}
\newenvironment{testTwo}{•}{•}

\begin{document}


First let's do two in the same environment:

\begin{testOne}
\fooCommand 

\fooCommand
\end{testOne}


Now, nested empty environments

\begin{testOne}
%First
    \begin{testTwo}
    \fooCommand 
    \end{testTwo}

%Second
    \begin{testTwo}
    \fooCommand
    \end{testTwo}

\end{testOne}

\end{document}

This outputs:

First let’s do two in the same environment:
The First is False!
The Second is True!
The First Is True!
The Second is True!
Now, nested empty environments
The First is False!
The Second is True!
The First is False!
The Second is True!

Notice how (since I never reset \fooLoggic to false) the first environment returns the first as false, and all others as true.

However, when I nest an empty environment (like in the second environment example) it does one false and one true each. So it appears that exiting the empty environment is resetting the \fooLogic check.

  1. Is this because it lacks a \global command on the logic set in the command \fooCommand?

  2. Is this just environments that reset them, or is it because I'm exiting a shell of any kind (environment, command, for loops, etc) that the value gets reset without a \global?

Hopefully this is vastly more readable/sensible. Sleep is helpful.

10
  • I don't understand what you are trying to do but rather than having \setcounter{fooCount}{0}, which sets footCount to zero, wouldn't it be better to have \refstcounter{fooCount} or \addtocounter{fooCount}{1}, which both increment fooCount?
    – user30471
    Dec 15, 2016 at 5:23
  • Sorry, let me clarify; The stepcounter is happening inside the FILE independently. And I need that count for something else. So at the end of the command I reset it to 0, so that if/when I call the command again later it will count correctly. In reality that FILE may be one of several, so I need to count the parts in the given file... it's not always the same number. That being said; is there a difference between refstcounter and stepcounter? I haven't been able to find any distinction.
    – Jason
    Dec 15, 2016 at 5:26
  • 1
    It would be far better for you to provide something that replicates the problem. We want to copy-and-paste-and-compile and see what you're experiencing. Yes, the actual code.
    – Werner
    Dec 15, 2016 at 5:42
  • If you use \refstepcount then it will be picked up by the next \label comment whereas if you use \stepcounter then the next \label will ignore the corresponding counter. As Werner says, it would be better if you can post a complete minimal working example that demonstrates your problem.
    – user30471
    Dec 15, 2016 at 5:56
  • If you do \newcommand{foo} you get an error; if you do \newenvironment{foo}{...}{...} and then try to do \newcommand{\foo}{...} you get an error as well. Please, make a sensible example, because this one isn't.
    – egreg
    Dec 15, 2016 at 7:16

1 Answer 1

1
  1. Spaces in the .tex input file behind something that gets tokenized as character token get tokenized as space tokens yielding (often unwanted) horizontal space in the pdf-file.

  2. \newcommand{foo}... does not work out. Must be \newcommand{\foo}....

  3. \newenvironment{foo} ... roughly speaking defines the commands \foo and \endfoo and the \begin{..}-\end{..}- mechanism executes these commands inside inside a local scope/group. Therefore \newenvironment{foo}{..}{..}...\newcommand\foo{... yields an error about \foo being already defined.

  4. fooCount is undefined.

  5. \input(FILE) should probably be something like \input{FILE}. (La)TeX usually uses curly braces not parentheses for macro arguments.

  6. If yout input file uses \ifthenelse then the package providing that command should be loaded.

  7. If a file is loaded several times by \input, then it should not contain \newcommand as at the second loading this will raise errors about the control sequence in question being defined already.

Having said these things, the following code might probably do to a certain degree what you want:

\documentclass{article}

% Here comes David Kastrup's \replicate macro, see
% <http://www.gust.org.pl/projects/pearls/2005p/david-kastrup/bachotex2005-david-kastrup-pearl3.pdf>
\newcommand\recur[1]{\csname rn#1\recur}%
\newcommand\rnm[1]{\endcsname{#1}#1}\newcommand\rn[1]{}
\newcommand\replicate[1]{\csname rn\expandafter\recur
\romannumeral\number\number#1 000\endcsname\endcsname}


%Let's create file.tex that executes 500 \goo{5}:
\begin{filecontents*}{file.tex}
%% Let's do 500 `\goo{5}`:
\replicate{500}{\goo{5}}%
\endinput
\end{filecontents*}

\usepackage{ifthen}

\newcounter{fooCount}

\newcommand{\goo}[1]{%
  \ifthenelse{#1 = 5}{%
    \stepcounter{fooCount}% Keep track of how many times #1 is 5
    %SomeCode
  }{}%
}%

\newenvironment{foo2}{%
  (This has some code that will hide some output, but not
   suppress it from running in the code)%
  \ignorespaces
}{\ignorespacesafterend}%

\newcommand{\foo}{%
  \input{file.tex}% FILE has a bunch of "\stepcounter{fooCount}"
  \arabic{fooCount}%
  \setcounter{fooCount}{0}%
}%

\newenvironment{foo3}{\ignorespaces}{\ignorespacesafterend}

\begin{document}

\begin{foo2}
\foo
\foo
\end{foo2}

\begin{foo2}
\begin{foo3}
\foo
\end{foo3}
\begin{foo3}
\foo
\end{foo3}
\end{foo2}

\end{document}

By the way:

Many LaTeX documents need to be compiled several times (at least twice) until \tableofcontents and all references and hyperlinks (in terms of \label{..} and \ref{..}or \pageref{..} work out. If you wish to have created portions of document's text randomly, you need to ensure that the ranom-thingie is done only during the first compilation and then is preserved for consecutive compilations.

Otherwise you might get a never ending story:

  1. (La)TeX-run yields (another) randomize-result.
  2. Other ranomize-result yields other text.
  3. Other text yields other pagebreaking.
  4. Other pagebreaking yields other page numbers.
  5. Other page numbers yield requiring another (La)TeX-run for having \tableofcontentsand \label..\ref matching out. You are back at 1.

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.