# How to make a list of environments?

I know that LaTeX/TeX is a complete programming language, but I can not find any topic about making list or vectors, as it is in C# or C++. My goal is to make a list, where I store names of my \newenvironment (for example let it be something like section), and refer to it by its number in the list.

The list size must not be constant it should depend on how much time the environment was called. Can this be done? (or should I make a program, which will manipulate the text and then compile it?)

I've wrote an example by writing external file and reading from it, and is it correct?

\documentclass{book}

\setlength\parindent{0pt}

\makeatletter
\newcommand{\Dotfill}{\leavevmode \cleaders \hb@xt@ .8em{\hss .\hss }\hfill \kern \z@}
\makeatother

\newwrite\contentfile
\immediate\openout\contentfile=\jobname.txt

\newcounter{Section}
\newenvironment{Section}[1]
{
\stepcounter{Section}
\vspace{10pt}
{\Large{$\S\theSection.$ #1}} \\[5pt]
\def\theSectionName {#1}

\immediate\write\contentfile{\string\S \theSection. \theSectionName \string\Dotfill \thepage\string\\}
}

\begin{document}
\pagestyle{empty}

\immediate\write\contentfile{\string\begin{center}}
\immediate\write\contentfile{\string\begin{minipage}{10cm}}

\Section{section one}

this is first

\Section{section two}

hjykfkk

\Section{section three}

hjykfkk dfga dfgagr gagergreghah

\newpage

\Section{section four}
hjykfkk dfga dfgagr gagergreghah

\immediate\write\contentfile{\string\end{minipage}}
\immediate\write\contentfile{\string\end{center}}
\immediate\closeout\contentfile
\input{\jobname.txt}

\end{document}

• That sounds like you simply want to label/ref stuff. – Johannes_B Dec 26 '15 at 11:55
• Well, I tried using them and searched examples, but I was not able to make them work as I needed. – Giorgi Dec 26 '15 at 11:58
• Don't get me wrong, but you rather invent the wheel once more than getting one of the most basic concepts of LaTeX to work? – Johannes_B Dec 26 '15 at 12:00
• The normal way to do that is to write each item out into a file and then read it back in. That's how tableofcontents etc work. If you want to make your own, then have a look at (for example) the tocloft package. – Thruston Dec 26 '15 at 12:20
• you give the example of \section and to generate a list of sections just use\tableofcontents after \begin{document} and run latex at least twice. If that is not what you mean, please make a complete small document and say what output you wish to make. – David Carlisle Dec 26 '15 at 12:28

Actually, I think the O.P. tries to reinvent the \addcontentsline and \addtocontents features, but those can be used without much ado, in my point of view. The Section is an example for any other environment.

\documentclass{book}

\setlength\parindent{0pt}

\makeatletter
\newcommand{\listofenvironments}{%
\section*{The environments}
\@starttoc{env}% Starting to show the environment list (file extension .env)
}
\makeatother

\newcounter{Section}

\newenvironment{Section}[1]
{%
\stepcounter{Section}  % should be \refstepcounter rather?
\vspace{10pt}
{\Large{$\S\theSection.$ #1}} \\[5pt] %Is this necessary?
% Write the number and enviroment name to the .env list file
}

\AtBeginDocument{%
}

\AtEndDocument{%
}

\begin{document}
\pagestyle{empty}

%\immediate\write\contentfile{\string\begin{center}}
%\immediate\write\contentfile{\string\begin{minipage}{10cm}}

\Section{section one}

this is first

\Section{section two}

hjykfkk

\Section{section three}

hjykfkk dfga dfgagr gagergreghah

\newpage

\Section{section four}
hjykfkk dfga dfgagr gagergreghah

\listofenvironments

\end{document}