0

I'd like to show some reference at begin of document with some conditions. Here is the example code

\documentclass{article}
\usepackage{hyperref}

\newcounter{ctest}
\setcounter{ctest}{0}
\newenvironment{test}{%
  \refstepcounter{ctest}%
  \label{test:\thectest}%
  \par\noindent\thectest.\hspace{1em}%
}{}

\begin{document}
Ref here with some conditions such as \verb|\thectest > 1|

\ref{test:2}
\ref{test:3}

\begin{test}
  first
\end{test}
\begin{test}
  second
\end{test}
\begin{test}
  third
\end{test}
\end{document}

enter image description here

Is there a way to write/append

\ifnum\thectest>1%
  \ref{test:\thectest}%
\fi%

to another file and input it in right place.

6
  • I don't understand the "write/append ... to another file and input it in right place" part. Why is "another file" there? – muzimuzhi Z Aug 19 '20 at 13:40
  • Something works like the contents. References are showed before, but conditions that judge whether to show the references are judged later so I think auxiliary file is needed. – ZhiyuanLck Aug 19 '20 at 13:44
  • 1
    You want to conditionally typeset something at the beginning of doc, based on the final value of some counter at the end of the same doc? – muzimuzhi Z Aug 19 '20 at 13:47
  • Yes this is what I want – ZhiyuanLck Aug 19 '20 at 13:49
  • What does "categories" represent? It is definitely a bad practice to edit the question to a more general form. It makes discussions added between two question editions useless and more or less off-topic. You can always ask a new and follow-up question. It's free and not that hard. – muzimuzhi Z Aug 19 '20 at 15:04
1

Package totcount is exactly for requirements like this.

totcount introduces a new consept "total counter". In the following example, \newtotcounter{ctest} creates a total counter and \totvalue{ctest} retrieves its total value (final value at the end of last compilation).

\documentclass{article}
\usepackage{totcount}

\newtotcounter{ctest}

\newenvironment{test}{%
  \refstepcounter{ctest}%
  \label{test:\thectest}%
  \par\noindent\thectest.\hspace{1em}%
}{}

\begin{document}
\ifnum\totvalue{ctest}>2\relax
  <true code>
\else
  <false code>
\fi

\begin{test} content \end{test}
\begin{test} content \end{test}
% \begin{test} content \end{test}
\end{document}

In case you are curious about a direct implementation, see the following example

\documentclass{article}
\usepackage{totcount}

\newcounter{ctest}
\newcounter{ctest@total}
\setcounter{ctest@total}{-1}

\makeatletter
\AtEndDocument{%
  \write\@auxout{\string\setcounter{ctest@total}{\arabic{ctest}}}%
}
\makeatother

\newenvironment{test}{%
  \refstepcounter{ctest}%
  \label{test:\thectest}%
  \par\noindent\thectest.\hspace{1em}%
}{}

\begin{document}
\ifnum\arabic{ctest@total}=-1\relax
  <a second run is required>
\else
  \ifnum\arabic{ctest@total}>2\relax
    <true code>
  \else
    <false code>
  \fi
\fi

\begin{test} content \end{test}
\begin{test} content \end{test}
% \begin{test} content \end{test}
\end{document}
6
  • What if the condition is not about the value of the counter but something else such as date and so on, is there a common way? – ZhiyuanLck Aug 19 '20 at 14:22
  • What does "so on" refer to? Do they, including the date, closely related to the current question? – muzimuzhi Z Aug 19 '20 at 14:28
  • Supporse the condition is more general. In detailed implemention of test environment, many extra information are assigned such as date and categories. And I will show the referrences by groups according to these information. – ZhiyuanLck Aug 19 '20 at 14:31
  • @ZhiyuanLck That's a second question. And please always provide detailed description and avoid fuzzy words as much as possible. – muzimuzhi Z Aug 19 '20 at 14:40
  • I did not realize there is a special solution for certain condition before I ask, but I think they can be thought as a single question, the latter is more general. – ZhiyuanLck Aug 19 '20 at 14:44
0

A rough version using expl3 and write file to \jobname .wq

\documentclass{article}
\usepackage{xparse}
\usepackage{hyperref}

\ExplSyntaxOn
\int_new:N \g_test_int
\int_gset:Nn \g_test_int {0}
\tl_new:N \g_test_tl
\iow_new:N \l_myout_iow
\AtEndDocument{
  \iow_open:Nn \l_myout_iow {\c_sys_jobname_str .wq}
  \iow_now:Nx \l_myout_iow {\token_to_str:N \def \token_to_str:N \toshow{\g_test_tl}}
  \iow_close:N \l_myout_iow
}

\NewDocumentEnvironment {test} {}
{
  \int_gincr:N \g_test_int
  \customlabel{test\c_colon_str\int_use:N \g_test_int}{\int_use:N \g_test_int}
  \int_compare:nT
    {\g_test_int > 1}
    {
      \tl_gput_right:Nx \g_test_tl {\token_to_str:N \ref{test:\int_use:N \g_test_int}}
    }
  \par\noindent\int_use:N \g_test_int.\hspace{1em}%
} {}

\NewDocumentCommand {\myinput} {} {
  \file_if_exist:nT { \c_sys_jobname_str .wq }
    { \file_input:n { \c_sys_jobname_str .wq } }
}
\ExplSyntaxOff
\makeatletter
\newcommand{\customlabel}[2]{%
  \protected@write \@auxout {}{\string \newlabel {#1}{{#2}{\thepage}{#2}{#1}{}} }%
  \hypertarget{#1}{}
}
\def\toshow{}
\makeatother

\begin{document}
Ref here with some conditions such as \verb|\thectest > 1|

\myinput
\toshow

\begin{test}
  first
\end{test}
\begin{test}
  second
\end{test}
\newpage
\begin{test}
  third
\end{test}
\end{document}

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.