निरंजन
Try this MWE -


\documentclass{article}
\newif\iftest
\AtEndDocument{%
\iftest
Hello world!
\else
Not working :(
\fi
}%

\begin{document}
{\testtrue}
\end{document}


I can see that as \testtrue is enclosed in a group, the final test done at the end of the document doesn't work, but I want it to work. So how to set a conditional to true value globally? i.e. even outside the local group.
Skillmon
Just for educational purposes, it is also possible to smuggle a conditional out of the current group using the primitive \aftergroup. The result is not a global assignment, but just a local assignment that escapes the current group level to the next one.

And again for educational purposes, also a loop that will smuggle the conditional to the top level (being a pretty slow version of global assignments; as I said this is for educational purposes, while the single level smuggling can be useful, this global smuggling should've been a global assignment from the start).


\documentclass[]{article}

\newif\iftest

\newcommand\evaltest{\iftest Hello world!\else Not working :(\fi}

\newcommand\smugglebool[1]
{%
\ifnum\currentgrouplevel>0
\expandafter\aftergroup
\csname #1\csname if#1\endcsname true\else false\fi\endcsname
\fi
}

\makeatletter
\newcommand\smuggleboolglobal[1]
{%
\ifnum\currentgrouplevel>\z@
\aftergroup\@smuggleboolglobal
\expandafter\aftergroup
\csname #1\csname if#1\endcsname true\else false\fi\endcsname
\fi
}
\newcommand\@smuggleboolglobal[1]
{%
#1%
\ifnum\currentgrouplevel>\z@
\aftergroup\@smuggleboolglobal
\aftergroup#1%
\fi
}
\makeatother

\begin{document}
\evaltest

{\testtrue\smugglebool{test}}\evaltest

{{\testfalse\smugglebool{test}}}\evaltest

{{\testfalse\smuggleboolglobal{test}}}\evaltest
\end{document}

निरंजन
Just after posting the question, I found an answer here <https://tug.org/pipermail/texhax/2014-November/021434.html>.


\documentclass{article}
\newif\iftest
\AtEndDocument{%
\iftest
Hello world!
\else
Not working :(
\fi
}%

\begin{document}
{\global\testtrue}
\end{document}


I still welcome alternative approaches. Don't hesitate to post more answers :)

Enter question or answer id or url (and optionally further answer ids/urls from the same question) from

Separate each id/url with a space. No need to list your own answers; they will be imported automatically.