Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

I made an environment named sol, that has a \refstepcounter command. I want to print on the first page of my document how many sol environments I have made in my document.

\documentclass[extrafontsizes,11pt,twocolumn,openany,openbib]{memoir}

\usepackage{makeidx}
\usepackage[T1]{fontenc}

\newcounter{sol}

\newenvironment{sol}[2]%
{\refstepcounter{sol}\index{\textbf{#2}}%
\addcontentsline{toc}{section}{\protect #1}%
\centering\textsc{\Large\textbf{#1}}\\%
\noindent By: \textbf{#2}\\%    
\begin{raggedright}\ignorespaces}%
{\end{raggedright}\ignorespacesafterend}

\begin{document}
This document consists of \thesol\ sols.
\tableofcontents
\twocolumn
.
.
.
\printindex
\end{document}

However, the value of the sol counter in the first page I always get is 0, since the there is no preceding sol environment from the \thesol command. So I came up, with the following solution:

\documentclass[extrafontsizes,11pt,twocolumn,openany,openbib]{memoir}

\usepackage{color}
\usepackage{makeidx}
\usepackage[T1]{fontenc}

\newcounter{sol}
\addtocounter{sol}{-1}

\newenvironment{sol}[2]%
{\refstepcounter{sol}\index{\textbf{#2}}%
\addcontentsline{toc}{section}{\protect #1}%
\centering\textsc{\Large\textbf{#1}}\\%
\noindent By: \textbf{#2}\\%    
\begin{raggedright}\ignorespaces}%
{\end{raggedright}\ignorespacesafterend}

\begin{document}
This document consists of \ref{sol:num}\ sols.
\tableofcontents
\twocolumn
.
.
.
{\color{white}
\begin{sol}{\color{white}a}{\color{white}a}
\label{sol:num}
\end{sol}}
\printindex
\end{document}

However, the new problem that arises is that the output of the code \color{white}a appears in the ToC and in the Index, as specified by the sol environment. Note: I placed the second issue in the same thread because I think the first one is not attainable, based on my present knowledge regarding LaTeX.

share|improve this question
    
The whole \ref \label \refstepcounter system (which includes the toc) works by writing data into a file which is read the next time LaTeX runs. However, what it stores depends on things like \@currentlabel so that it knows whether to store equation, references, chapters, etc. –  John Kormylo May 25 at 14:47
    
@JohnKormylo - Thanks, but what can I do to print those stored values? –  Acnologia May 25 at 14:52
    
@Acnologia: The title of your question is a bit misleading: You mean the last \refstepcounter of sol counter and not the very last call to \refstepcounter of any counter in the document. –  Christian Hupfer May 25 at 15:33
    
Yes, @ChristianHupfer. I think I am referring to the second one. –  Acnologia May 27 at 8:34

4 Answers 4

up vote 3 down vote accepted

The package totcount has exactly this purpose:

\documentclass{memoir}
\usepackage{totcount}

\usepackage{lipsum} % just for the example

\newcounter{sol}
\regtotcounter{sol}

\newenvironment{sol}[2]
  {\refstepcounter{sol}\index{\textbf{#2}}%
   \addcontentsline{toc}{section}{#1}%
   \centering{\Large\scshape #1\\}
   \raggedright
   By: \textbf{#2}\\
   \ignorespaces}
  {\par}

\begin{document}
This document consists of \total{sol} sols.

\begin{sol}{Abc}{Def}
\lipsum*[2]
\end{sol}

Another

\begin{sol}{Abc}{Def}
\lipsum*[3]
\end{sol}

\end{document}

enter image description here

share|improve this answer
    
This was the simplest solution I have ever tumbled on regarding this problem. In the first run, I have encountered no errors. Thanks! –  Acnologia May 27 at 10:33

Another solution: Use the LastPage trick for another counter:

Write the value of the sol counter to the .aux file at the end of the document and rerun (which is to be done basically always) pdflatex (or latex) -- it reads the number stored in TotalSolEnvs and can be output wherever needed.

The \AtEndDocument command executes commands at the end of the document, so the command \WriteNumberOfSolEnvs too, being responsible for storing the number of used solution environments.

This mechanism works for other counters too.

\documentclass[extrafontsizes,11pt,twocolumn,openany,openbib]{memoir}

\usepackage{color}
\usepackage{makeidx}
\usepackage[T1]{fontenc}

\newcounter{sol}

\newcounter{TotalSolEnvs}%

\makeatletter
\newcommand{\WriteNumberOfSolEnvs}{%
\immediate\write\@auxout{%
  \string\setcounter{TotalSolEnvs}{\number\value{sol}}
}
}
\makeatother


\AtEndDocument{%
  \WriteNumberOfSolEnvs%
}%

\newenvironment{sol}[2]%
{\refstepcounter{sol}\index{\textbf{#2}}%
\addcontentsline{toc}{section}{\protect #1}%
\centering\textsc{\Large\textbf{#1}}\\%
\noindent By: \textbf{#2}\\%    
\begin{raggedright}\ignorespaces}%
{\end{raggedright}\ignorespacesafterend}

\begin{document}
This document consists of \number\value{TotalSolEnvs}~ sols.
\tableofcontents
\twocolumn
\begin{sol}{Arg1}{Arg2}%
\end{sol}

\begin{sol}{Arg1}{Arg2}%
\end{sol}

\begin{sol}{Arg1}{Arg2}%

\end{sol}

\begin{sol}{Arg1}{Arg2}%
\end{sol}


\printindex
\end{document}

enter image description here

share|improve this answer
    
It worked for me! This is what I'm thinking to do, to deceive my LaTeX code that that part is the last page, but I can't do. However, I suggest @ChristianHupfer to simplify \number\value{TotalSolEnvs} to \theTotalSolEnvs. The \the<counter> command gives the numerical value of a counter. I tested my suggestion and it worked! –  Acnologia May 27 at 10:08
    
@Acnologia: Well, there is one little caveat: As soon as \theTotalSolEnvs is redefined to output the counter number (and not the numerical value!!!) in Roman or in upcase/lowcase letters, you will get I or F for example but not 1 or 6 respectively. \number\value{TotalSolEnvs} however will give you the numerical value ;-) –  Christian Hupfer May 27 at 11:34

Add this to your preamble:

\usepackage{refcount}
\newcommand{\manuallabel}[2]{\def\@currentlabel{#2}\label{#1}}
\newcounter{sol}
\addtocounter{sol}{0}

\newenvironment{sol}[2]%
{\refstepcounter{sol}\index{\textbf{#2}}%
\addcontentsline{toc}{section}{\protect #1}%
\centering\textsc{\Large\textbf{#1}}\\%
\noindent By: \textbf{#2}\\%    
}

and this to the end of your document:

\manuallabel{counter}{\number\numexpr\getrefnumber{solref@\number\value{sol}}}

Now you can use

This document consists of \ref{counter} sols.

\sol{1}{2}

You can use different names by changing the following two lines:

\manuallabel{NAME}{...
This document consists of \ref{NAME} sols.

Example:

\documentclass[extrafontsizes,11pt,twocolumn,openany,openbib]{memoir}

\usepackage{color}
\usepackage{makeidx}
\usepackage[T1]{fontenc}

\usepackage{refcount}
\newcommand{\manuallabel}[2]{\def\@currentlabel{#2}\label{#1}}
\newcounter{sol}
\addtocounter{sol}{0}

\newenvironment{sol}[2]%
{\refstepcounter{sol}\index{\textbf{#2}}%
\addcontentsline{toc}{section}{\protect #1}%
\centering\textsc{\Large\textbf{#1}}\\%
\noindent By: \textbf{#2}\\%    
}

\begin{document}
This document consists of \ref{counter} sols.
\tableofcontents

\sol{1}{1}
\sol{1}{1}

\manuallabel{counter}{\number\numexpr\getrefnumber{solref@\number\value{sol}}}
\end{document}

This document consists of 2 sols.

share|improve this answer
    
Typing this code: This document consists of \thesol\ sols. Still results to: This document consists of 0 sols. –  Acnologia May 27 at 9:49
    
Oh sorry, you have to use This document consists of \ref{counter} sols. I've edited my answer. –  SLx64 May 27 at 10:02
    
I implemented the change you suggested in the code. But it resulted to This document consists of sols. Another thing, do I really need to add \addtocounter{sol}{0}? As far as I know, its purpose is only to add 0 to sol counter. –  Acnologia May 27 at 10:19
    
No, you don't need to add this line but you can start with different values. –  SLx64 May 27 at 10:24
    
Ah, I see. I think you have turned sol into a command, but I need it to be an environment. –  Acnologia May 27 at 10:42
\documentclass[extrafontsizes,11pt,twocolumn,openany,openbib]{memoir}

\usepackage{makeidx}
\usepackage[T1]{fontenc}

\makeatletter% I stole this, more or less, from hyperref
\def\total{%
  \@bsphack
  \begingroup
    \protected@write\@auxout{}{%
      \string\newlabel{total}{%
        {}% not used
        {\thepage}%
        {}% not used
        {\arabic{sol}}{}%
      }%
    }%
  \endgroup
  \@esphack
}%
\makeatother

\newcounter{sol}

\newenvironment{sol}[2]%
{\stepcounter{sol}\index{\textbf{#2}}%
\addcontentsline{toc}{section}{\protect #1}%
\centering\textsc{\Large\textbf{#1}}\\%
\noindent By: \textbf{#2}\\%    
\begin{raggedright}\ignorespaces}%
{\end{raggedright}\ignorespacesafterend}

\begin{document}
This document consists of \ref{total} sols.
\tableofcontents

\begin{sol}{First}{Golly}
Body of text.
\end{sol}

\begin{sol}{Second}{Gosh}
More text.
\end{sol}

\total
\printindex
\end{document}
share|improve this answer
    
I obtained errors from building this code, which both say that total is an undefined reference. I tried replacing \total with \label{total} but the error still persisted (after building thrice). –  Acnologia May 27 at 8:53
    
Haha, ok, disregard my previous comment. I worked on the code again and it produced a pdf file, but the code This document consists of \ref{total} sols. resulted to This document consists of 5 sols. I think it is due to \setcounter{sol}{5} macro. –  Acnologia May 27 at 9:48
    
Yes, I didn't want to get 0 sols and was in a hurry. I will correct it. –  John Kormylo May 27 at 12:08
    
BTW, you can use \stepcounter instead of \refstepcounter. –  John Kormylo May 27 at 12:33

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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