How can I have a command like \copypaste{ref-label} such that it copy the corresponding environment to label \label{ref-label} and paste it where the command \copypaste{ref-label} is? or at least (if this is easier) two commands \copy{ref-label} (placing inside of the environment) and \paste{ref-label} (where we want to paste)?

I know that this commands may be useless but it is interesting and useful for me because sometimes I want to quote some lemmas, definitions,... from earlier chapters. Of course I can define new environments (new def, lem ,thm, ...) and set their counter to corresponding environment.

  • The \label-command is already defined and the concepts belonging to the \label..\ref-mechanism are well established since decades and therefore should not be distorted. You'd need to give your command another name. In any case a problem is: When you have LaTeX (re)set whatsoever counters to former values, the likelihood is high that this will break the hyperref package which forms names of destinations/referencing anchors from the current values and names of the corresponding counters while names of referencing anchors must be unique. Apart from that you could .. (see my next comment) – Ulrich Diez Oct 1 '19 at 17:33
  • (this is my next comment) ... Apart from that you could have LaTeX read things under verbatim-category-code régime (probably taking horizontal tab into account) and store things (including counter-values) in a macro and for referencing apply \scantokens to the expansion of that macro. This could even be done with an intermediate step via the .aux-file so that referencing before the thing occurs in the main text is possible. – Ulrich Diez Oct 1 '19 at 17:33
  • @C.F.G has reviewed the answer that I proposed? – Pablo González L Dec 2 '19 at 3:53
  • @PabloGonzálezL: Unfortunately, package in your answer, needs texlive 2019 but I have texlive 2018 and your answer don't works for me. Also I tried to install v 2019 but failed. see this post. – C.F.G Dec 2 '19 at 4:42

Something like that:

Some text here...

First definition stored in memory.

More text here...

Second definition stored in memory.

\section{Other section}
Another little bit of text here, now I bring the definitions
I saved in memory...


In reverse order


\section{One more time}
Okay, I'll reoccupy the definitions, but in the right order.



The scontents package is designed to do this job, I have used the environment version, but you can also use the command version. If you have verbatim material is also supported and if you want to write to external files, well, you can also do it :).

| improve this answer | |
  • Finally I installed TexLive 2019. When I use Theorem env. it works very well but it changes the theorem counter of stored Theorem env. – C.F.G Dec 3 '19 at 6:36
  • \begin{scontents}[print-env=true,store-env=definitions] \begin{theorem} Theorem stored in memory. \end{theorem} \end{scontents} – C.F.G Dec 3 '19 at 6:36
  • @C.F.G I think you need to renew the associated counter before running getstored (untested). – Pablo González L Dec 3 '19 at 10:24
  • How to renew the associated counter without effecting on next theorem? It seems that there is no other solution for my question. so I accept your answer. – C.F.G Dec 3 '19 at 10:40
  • 1
    @C.F.G If only theorems and definitions are involved, the most appropriate approach would be to use thmtools and thm-restate, perhaps this can help you (tex.stackexchange.com/a/51288/7832). – Pablo González L Dec 3 '19 at 11:37

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.