LaTeX forum ⇒ GeneralAdd appendix of Theroems

LaTeX specific issues not fitting into one of the other forums of this category.
Mikerada6
Posts: 42
Joined: Fri Oct 17, 2008 5:55 pm

Add appendix of Theroems

Postby Mikerada6 » Fri Oct 17, 2008 5:56 pm

Is there a way to make an Appendix of all the theorems i have wrote in the paper? So that all theorems appear one after another much like an index of them?

User avatar
Juanjo
Posts: 657
Joined: Sat Jan 27, 2007 12:46 am

Postby Juanjo » Sun Oct 19, 2008 1:11 am

I think it is not clear from your post what you are searching for. Do you want the full statement of every result or just a brief reference? As a first idea of what can be achieved, compile the following code:
  1. \documentclass[a4paper]{book}
  2. \usepackage{amsthm}
  3. \usepackage{tocloft}
  4. \usepackage{lipsum}
  5.  
  6. \newcommand{\listofresultsname}{List of Results}
  7. \newlistof[chapter]{results}{res}{\listofresultsname}
  8.  
  9. \newtheorem{theorem}{Theorem}[chapter]
  10. \newtheorem{corollary}[theorem]{Corollary}
  11.  
  12. \newenvironment{mytheorem}%
  13. {\begin{theorem}%
  14. \addcontentsline{res}{results}{Theorem \protect\numberline{\thetheorem}}}%
  15. {\end{theorem}}
  16. \newenvironment{mycorollary}%
  17. {\begin{corollary}%
  18. \addcontentsline{res}{results}{Corollary \protect\numberline{\thetheorem}}}%
  19. {\end{corollary}}
  20.  
  21. \begin{document}
  22. \listofresults
  23.  
  24. \chapter{First chapter}
  25.  
  26. \begin{mytheorem}
  27. \lipsum[1]
  28. \end{mytheorem}
  29.  
  30. \begin{mytheorem}
  31. \lipsum[2]
  32. \end{mytheorem}
  33.  
  34. \begin{mycorollary}
  35. \lipsum[3]
  36. \end{mycorollary}
  37.  
  38. \begin{mytheorem}
  39. \lipsum[4]
  40. \end{mytheorem}
  41.  
  42. \chapter{Second chapter}
  43.  
  44. \begin{mytheorem}
  45. \lipsum[1]
  46. \end{mytheorem}
  47.  
  48. \begin{mytheorem}
  49. \lipsum[2]
  50. \end{mytheorem}
  51.  
  52. \begin{mycorollary}
  53. \lipsum[3]
  54. \end{mycorollary}
  55.  
  56. \begin{mytheorem}
  57. \lipsum[4]
  58. \end{mytheorem}
  59.  
  60. \end{document}

Things can be greatly improved, but, at the same time, they become more complex, depending on the class of document you use, the theorem-like environments you define, their numbering, the use of starred versions of such environments, the use of their optional arguments, etc. Think precisely what you want and consider if it is worthy to invest time and effort in it.
The CTAN lion is an artwork by Duane Bibby. Courtesy of www.ctan.org.

Mikerada6
Posts: 42
Joined: Fri Oct 17, 2008 5:55 pm

Postby Mikerada6 » Sun Oct 19, 2008 2:01 am

the preamble to my documernt is
  1. % LaTeX Book Template - using defaults
  2. \documentclass[openright]{book}
  3. \usepackage{latexsym}
  4. \usepackage{enumerate}
  5. \usepackage{amsmath,amssymb}
  6. \usepackage[amsmath,amsthm]{ntheorem}
  7. \usepackage{graphics}
  8. \usepackage{makeidx}
  9. \theoremstyle{remark} \newtheorem{ex}{\textsuperscript{ex}}
  10. \theoremstyle{remark} \newtheorem{note}{\textsuperscript{note}}
  11. \newtheorem{thm}{Theorem}
  12. \newtheorem{lem}{Lemma}
  13. \newtheorem{col}{Corollary}
  14. \theoremstyle{definition}\newtheorem{defn}{Definition}[chapter]
  15. \renewcommand{\contentsname}{Table of Contents}
  16. \makeindex


and yes i am trying to get a total restatement of everything that is listed under the
  1. \begin{thm} \end{thm}
and then if possible a second appendix with everything under
  1. \begin{defn} \end{defn}

User avatar
Juanjo
Posts: 657
Joined: Sat Jan 27, 2007 12:46 am

Postby Juanjo » Mon Oct 20, 2008 1:19 am

Now it is more clear what you want. It is quite similar to the problem considered in this other topic, where a user wanted a list of equations. I've adapted and improved the code there to show you things which can be done. Consider the following code:
  1. \documentclass[a4paper]{report}
  2. \usepackage{amsthm,amsfonts}
  3. \newtheorem{theorem}{Theorem}[chapter]
  4. \newtheorem{corollary}[theorem]{Corollary}
  5. \theoremstyle{definition}
  6. \newtheorem{definition}{Definition}[chapter]
  7.  
  8. % --------------------------------------------
  9. % --------------------------------------------
  10. % Select the appropriate switch depending on your operating system
  11. \newif\ifOSisUnix
  12. \OSisUnixtrue % Unix, Linux, Mac OS X...
  13. % \OSisUnixfalse % Windows
  14.  
  15. % Load fancyvrb
  16. \usepackage{fancyvrb}
  17.  
  18. % Name of the scratch file: \ScratchFile is <jobname>.tmp
  19. \newcommand{\ScratchFile}{\jobname.tmp}
  20.  
  21. % Command \SetTempFile to set names of auxiliary files where to store code.
  22. % \SetTempFile{foo} defines \fooTempFile as <jobname>-foo.tmp and writes in this file
  23. % the line \let\label\@gobble (this avoids warnings about multiply defined labels).
  24. % Any existing file \fooTempFile is overwritten.
  25.  
  26. \newwrite\outputstream
  27. \makeatletter
  28. \newcommand{\SetTempFile}[1]{%
  29. \@namedef{#1TempFile}{\jobname-#1.tmp}%
  30. \immediate\openout\outputstream=\@nameuse{#1TempFile}%
  31. \immediate\write\outputstream{\string\makeatletter}%
  32. \immediate\write\outputstream{\string\let\string\label\string\@gobble}%
  33. \immediate\write\outputstream{\string\makeatother}%
  34. \immediate\closeout\outputstream}
  35. \makeatother
  36.  
  37. % Command \AppendScratchFile.
  38. % \AppendScratchFile{foo} appends the contents of \ScratchFile to those of
  39. % the auxiliary file \fooTempFile
  40. \makeatletter
  41. \newcommand{\AppendScratchFile}[1]{%
  42. \ifOSisUnix%
  43. \immediate\write18{cat \ScratchFile >> \@nameuse{#1TempFile}}%
  44. \else%
  45. \immediate\write18{type \ScratchFile >> \@nameuse{#1TempFile}}%
  46. \fi}
  47. \makeatother
  48.  
  49. % Environment StoreCode. Usage:
  50. % \begin{StoreCode}{foo}
  51. % code
  52. % \end{StoreCode}
  53. % It typesets all the code that lies inside and saves it to the auxiliary file \fooTempFile.
  54. % The definition is a bit tricky. It is not done through \newenvironment
  55.  
  56. \makeatletter
  57. \newcommand{\StoreCode}[1]%
  58. {\gdef\TempFileSuffix{#1}%
  59. \VerbatimEnvironment
  60. \begin{VerbatimOut}{\ScratchFile}}
  61. \def\endStoreCode{%
  62. \end{VerbatimOut}
  63. \input{\ScratchFile}%
  64. \AppendScratchFile{\TempFileSuffix}}
  65. \makeatother
  66.  
  67. % Command \StoreCounterValue.
  68. % \StoreCounterValue{count}{foo} writes in the file \fooTempFile the line
  69. % \setcounter{count}{counter_value}. It is useful to control numbering the second
  70. % time the code is processed.
  71.  
  72. \newcommand{\StoreCounterValue}[2]{%
  73. \immediate\openout\outputstream=\ScratchFile%
  74. \immediate\write\outputstream{\string\setcounter{#1}{\arabic{#1}}}%
  75. \immediate\closeout\outputstream%
  76. \AppendScratchFile{#2}}
  77.  
  78. % Command \InputTempFile.
  79. % \InputTempFile{foo} inputs the file \fooTempFile
  80. \makeatletter
  81. \newcommand{\InputTempFile}[1]{\input{\@nameuse{#1TempFile}}}
  82. \makeatother
  83.  
  84. % --------------------------------------------
  85. % --------------------------------------------
  86.  
  87. % Auxiliary macro for writing maths
  88. \newcommand{\Rset}{\mathbb{R}}
  89.  
  90. % We set the auxiliary files for storing code: thm for theorems and def for definitions.
  91. \SetTempFile{thm}
  92. \SetTempFile{def}
  93.  
  94. \begin{document}
  95.  
  96. \chapter{First chapter}
  97.  
  98. \StoreCounterValue{chapter}{thm}
  99. \StoreCounterValue{theorem}{thm}
  100. \StoreCounterValue{chapter}{def}
  101.  
  102. \begin{StoreCode}{def}
  103. \begin{definition}\label{def:1}
  104. A function $f:\Rset\to\Rset$ is \emph{continuous} in a point
  105. $x_0$ if \[\lim_{x\to x_0}f(x)=f(x_0).\]
  106. \end{definition}
  107. \end{StoreCode}
  108.  
  109. \begin{StoreCode}{def}
  110. \begin{definition}\label{def:2}
  111. A funci\'on $f:\Rset\to\Rset$ is \emph{derivable} in
  112. a point $x_0$ if \[\lim_{x\to x_0}\frac{f(x)-f(x_0)}{x-x_0}\]
  113. exists and is finite. In such a case, this limit is called the
  114. \emph{derivative} of $f$ in $x_0$ and is denoted by $f'(x_0)$.
  115. \end{definition}
  116. \end{StoreCode}
  117.  
  118. The following result states the fundamental relation existing between the notions introduced in
  119. Definitions \ref{def:1} and \ref{def:2}.
  120.  
  121. \begin{StoreCode}{thm}
  122. \begin{theorem}
  123. If $f:\Rset\to\Rset$ is derivable in $x_0$, then $f$ is continuous in $x_0$.
  124. \end{theorem}
  125. \end{StoreCode}
  126.  
  127. \chapter{Second chapter}
  128.  
  129. \StoreCounterValue{chapter}{thm}
  130. \StoreCounterValue{theorem}{thm}
  131.  
  132. \begin{StoreCode}{thm}
  133. \begin{theorem}[Rolle's Theorem]
  134. Let $f:[a,b]\to\Rset$ be a function continuous on $[a,b]$, derivable on
  135. $(a,b)$, and such that $f(a)=f(b)$. Then, there exists $c\in(a,b)$ such that
  136. $f'(c)=0$.
  137. \end{theorem}
  138. \end{StoreCode}
  139.  
  140. \begin{StoreCode}{thm}
  141. \begin{theorem}[Mean Value Theorem]
  142. Let $f:[a,b]\to\Rset$ be a function continuous on $[a,b]$ and derivable on
  143. $(a,b)$. Then, there exists $c\in(a,b)$ such that $f(b)-f(a)=f'(c)(b-a)$.
  144. \end{theorem}
  145. \end{StoreCode}
  146.  
  147. \begin{corollary}
  148. Let $f:[a,b]\to\Rset$ be a function continuous on $[a,b]$ and derivable on
  149. $(a,b)$. If, for all $x\in(a,b)$, $f'(x)\geq0$, then $f$ is increasing in $[a,b]$.
  150. \end{corollary}
  151.  
  152. \StoreCounterValue{theorem}{thm}
  153.  
  154. \begin{StoreCode}{thm}
  155. \begin{theorem}[Cauchy's Mean Value Theorem]
  156. Let $f, g:[a,b]\to\Rset$ be two functions continuous on $[a,b]$ and derivable on
  157. $(a,b)$. Then, there exists $c\in(a,b)$ such that $g'(b)(f(b)-f(a))=f'(c)(g(b)-g(a))$.
  158. \end{theorem}
  159. \end{StoreCode}
  160.  
  161. \chapter*{Main definitions}
  162. \InputTempFile{def}
  163.  
  164. \chapter*{Main results}
  165. \InputTempFile{thm}
  166.  
  167. \end{document}

The code contains comments explaining macros and their usage. Feel free to ask if you don't understand something. Essentially, the code for theorems and definitions is stored in auxiliary files (with extension tmp) and processed two times: once, when the code appears, and later, in the appendix.

I've tested it on Mac OS X (Tex Live 2008) and Windows (MikTeX 2.7) and works perfectly in both cases. I attach the corresponding pdf file. Select the correct value for the switch \ifOSisUnix and be sure that the \write18 feature is enabled (that's the case, by default, on a Mac). A forum search of "write18" will bring you more information on that feature.
Attachments
pru.pdf
(53.06 KiB) Downloaded 104 times
The CTAN lion is an artwork by Duane Bibby. Courtesy of www.ctan.org.

Mikerada6
Posts: 42
Joined: Fri Oct 17, 2008 5:55 pm

Postby Mikerada6 » Mon Oct 20, 2008 5:10 pm

This is EXACTLY what i want. You are amazing. I am having two problems though. Right before i write to chapters that have been made i have
  1. \appendix
  2. \chapter{Definitions}
  3. \InputTempFile{def}
  4. \chapter{Theorems}
  5. \InputTempFile{thm}
  6. \chapter{Important Graphs}

this causes
Appendix A: Definitions
Appendix B: Theorems
Appendix B: Important Graphs

for some reason it is not counting Theorems as an appendix and lettering two different appendices as "B"

The second problem is that all the definations come out labeled as A.1, A.2. i think this has something to do with my placemnt of the code
  1. \StoreCounterValue{chapter}{thm}
  2. \StoreCounterValue{theorem}{thm}
  3. \StoreCounterValue{chapter}{defn}

I am not quite sure what this bit of code does, and where i should place it.

User avatar
Juanjo
Posts: 657
Joined: Sat Jan 27, 2007 12:46 am

Postby Juanjo » Mon Oct 20, 2008 7:36 pm

Replace the comments and defintion of \InputTempFile by the following code:
  1. % Command \InputTempFile.
  2. % \InputTempFile{foo} inputs the file \fooTempFile. It is assumed that this is done in an appendix
  3. % chapter (so the \appendix command has already been issued). We initially need to save the actual
  4. % value of the chapter counter and change its format to arabic (used in the main chapters). Then
  5. % the contents of \fooTempFile are processed. Finally, the value and format of the chapter counter
  6. % is restored.
  7. \newcounter{TempChapter}
  8. \makeatletter
  9. \newcommand{\InputTempFile}[1]{%
  10. \setcounter{TempChapter}{\value{chapter}}%
  11. \renewcommand{\thechapter}{\arabic{chapter}}%
  12. \input{\@nameuse{#1TempFile}}%
  13. \setcounter{chapter}{\value{TempChapter}}%
  14. \renewcommand{\thechapter}{\Alph{chapter}}}
  15. \makeatother

I hope that now everything will work fine.

A command like \StoreCounterValue{chapter}{thm} just saves the actual value of the chapter counter (through a \setcounter command) in the temp file with suffix thm. Look the contents of the temp files. I'll try to explain the sense of such a command. Suppose that you write a long book with many chapters. Suppose that, say, L'Hôpital's Rule is stated as Theorem 7.5 (that is, the fifth result using counter theorem in Chapter 7). When LaTeX processes the appendix, before rewritting theorems in Chapter 7, it needs to know that they belong to that chapter, that is, that the value of the chapter counter is 7. A simple way to tell to LaTeX this information is to put \setcounter{chapter}{7} in the temp file before the code of any theorem in Chapter 7. This is precisely the role of \StoreCounterValue{chapter}{thm}.

Likewise, before typesetting L'Hôpital's Rule, LaTeX needs to know that the theorem counter is 4. Thus, when processing
\begin{theorem}[L'H\^opital's Rule], LaTeX increments the theorem counter and correctly writes "Theorem 7.5". If you have stored the code of the four previous results (asuming that the numbering of the first one is correct), nothing is needed, since each \begin{theorem} increments the theorem counter. However, if you skip one result, you need to explicitly tell the correct value of that counter, so you need a \StoreCounterValue command. As a rule ot thumb:
* in each chapter containing theorems you want to rewrite in the appendix, add \StoreCounterValue{chapter}{thm} after \chapter,
* before every theorem that you find having an incorrect theorem number in the appendix, add \StoreCounterValue{theorem}{thm}.

The same rules applies for definitions. In case of doubt, add \StoreCounterValue and see the effect.
The CTAN lion is an artwork by Duane Bibby. Courtesy of www.ctan.org.


Return to “General”

Who is online

Users browsing this forum: No registered users and 1 guest