4

What would be an elegant way to add (in my case tables) to the appendix without leaving the part of the file in am working on? i.e. how should I define the \addtoappendix command in:

Here is an example of a table you can find in \cref{appendix} \addtoappendix{table}.

I have seen this question, is there maybe something easier/shorter?

The setting is as follows: I am using Pythontex to write the tables and I would like to keep a copy of these in the appendix.

EDIT (minimal examples):

First without Pythontex:

\documentclass[11pt,a4paper]{report}
\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}
\usepackage{amsmath}
\usepackage{amsthm, amssymb}
\usepackage{amsfonts}
\usepackage{hyperref}
\usepackage[nameinlink]{cleveref}

\begin{document}

\begin{table}
  \center
\begin{tabular}{cc}
  $a$&$b$\\
  $c$&$d$
\end{tabular}
\caption{A table}
\label{tab:table}
\end{table}
Here is a \cref{tab:table}. 
\end{document}

In this case, \addtoappendix{\begin{table}...\end{table}} should print the table in the appendix.

I think adapting this with the Pythonthex should be straighforward, but maybe not:

\documentclass[11pt,a4paper]{report}
\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}
\usepackage{amsmath}
\usepackage{amsthm, amssymb}
\usepackage{amsfonts}
\usepackage{hyperref}
\usepackage{pythontex}
\usepackage[nameinlink]{cleveref}

\begin{document}

\begin{pycode}
def ptable():
  print("\\begin{table}\\center")
  print("\\begin{tabular}{cc}")
  print("$a$&$b$\\\\")
  print("$c$&$d$")
  print("\\end{tabular}")
  print("\\caption{A table}")
  print("\\label{tab:table}")
  print("\\end{table}")
\end{pycode}
\pyc{ptable()}
Here is \cref{tab:table}

\end{document}

\addtoappendix{\pyc{ptable()}} should print the table in the appendix.

Thank you!

Nre
  • 222

1 Answers1

2

You can do it by collecting the tables in a token list register, with the help of environ.

\documentclass[11pt,a4paper]{report}
\usepackage[T1]{fontenc}
\usepackage[utf8]{inputenc}
\usepackage{amsmath}
\usepackage{amsthm, amssymb}
\usepackage{environ}
\usepackage{hyperref}
\usepackage[nameinlink]{cleveref}

\usepackage{lipsum} % for context

\newtoks\appendixtabletoks

\NewEnviron{appendixtable}{%
  \edef\temp{%
    \noexpand\begin{table}[hp]
    \unexpanded\expandafter{\BODY}
    \noexpand\end{table}
  }%
  \global\appendixtabletoks=\expandafter{%
    \the\expandafter\appendixtabletoks\temp
  }
}

\begin{document}

\lipsum[1]

\begin{appendixtable}
\centering
\begin{tabular}{cc}
  x & y \\
  abc & def
\end{tabular}
\caption{A table}
\label{tab:table}
\end{appendixtable}
Here is a \cref{tab:table}.

\appendix
\chapter{Tables}

\the\appendixtabletoks

\end{document}

enter image description here

egreg
  • 1,121,712