![如何直接在 PDF 中写入 pdf 对象并通过 LaTeX 或 expl3 获取该对象的编号?](https://linux22.com/image/403367/%E5%A6%82%E4%BD%95%E7%9B%B4%E6%8E%A5%E5%9C%A8%20PDF%20%E4%B8%AD%E5%86%99%E5%85%A5%20pdf%20%E5%AF%B9%E8%B1%A1%E5%B9%B6%E9%80%9A%E8%BF%87%20LaTeX%20%E6%88%96%20expl3%20%E8%8E%B7%E5%8F%96%E8%AF%A5%E5%AF%B9%E8%B1%A1%E7%9A%84%E7%BC%96%E5%8F%B7%EF%BC%9F.png)
通过 Lua,我可以直接在 PDF 中编写和使用对象:
local num = pdf.immediateobj("stream", "\\(a^2+b^2=c^2\\)", "/Type /SomeType /Subtype /SomeSubtype")
我如何通过expl3
简单的 LaTeX 命令执行相同操作以使其在 pdfLaTeX 和 LuaLaTeX 中工作?
答案1
您可以使用 l3pdf 命令。我这样做了不是检查流内容的语法是否正确:
\documentclass{article}
\usepackage{l3pdf}
\ExplSyntaxOn
\pdf_uncompress:
\ExplSyntaxOff
\begin{document}
\ExplSyntaxOn
\pdf_object_new:nn {mystream}{stream}
\pdf_object_write:nn {mystream}
{{/Type /SomeType /Subtype /SomeSubtype}
{\(a^2+b^2=c^2\)}}
\pdf_object_ref:n {mystream}
some text ...
%to get only the number as requested in the comments
% make only sense for pdflatex and lualatex
\tl_set:Nx\l_tmpa_tl { \pdf_object_ref:n {mystream} }
\seq_set_split:NnV \l_tmpa_seq {~} { \l_tmpa_tl }
\seq_item:Nn\l_tmpa_seq{1}
\ExplSyntaxOff
\end{document}