1
Here is page \pageref{here}\label{here} 

\pageref{here}\label{here} can get the page number of the current location, I want to write it into a txt file, but writing directly will report an error, I want to know how to convert this variable so that it can be written into a txt file.

\documentclass{article} \usepackage{zref-abspage,lipsum} \usepackage{fancyhdr} \newwrite\myoutfile \immediate\openout\myoutfile=\jobname-myoutfile.txt \begin{document} \lipsum[1-10] \immediate\write\myoutfile{\pageref{here}\label{here}.} \lipsum[10-12] \end{document} 
5
  • it is already written to the aux file, you could simply extract it from there with grep here foo.aux or similar Commented Dec 9, 2021 at 9:10
  • but if you mean write that literal text (rather than the page number) then \immediate\write\myoutfile{\detokenize{\pageref{here}\label{here}.}} Commented Dec 9, 2021 at 9:12
  • But what I want to write is the page number obtained by this command Commented Dec 9, 2021 at 10:26
  • You can use zref or refcount to get expandable page references. Commented Dec 9, 2021 at 11:08
  • you don't need a label for that just use \write\myoutfile{\thepage}, but as I said in the first comment, why do you need a special file for this, if you do use \label then the information is written to the aux file. Commented Dec 9, 2021 at 11:42

1 Answer 1

1

I added !!! just for debugging so you can see where the page write happened, on page 2 in this case:

enter image description here

The file written is

This was on page 2. 

You just need to do a non-immediate \write to make sure that you get the correct number

\documentclass{article} \usepackage{zref-abspage,lipsum} \usepackage{fancyhdr} \newwrite\myoutfile \immediate\openout\myoutfile=\jobname-myoutfile.txt \begin{document} \lipsum[1-10] !!!\write\myoutfile{This was on page \thepage.} \lipsum[10-12] \end{document} 

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.