在使用 newfile 读写文件之前评估 pgfkey

在使用 newfile 读写文件之前评估 pgfkey

我想定义一个自定义辅助文件,其中包含一些存储在 pgfkey 中的数据。这些数据必须从密钥中存储,然后写入密钥。

但是,当我查看 aux 文件时,它不包含我保存的数据,而是内部变量。读取也不起作用,因为它似乎将 \readaline 函数存储在键中,而不是评估后的输出(此部分已被注释)。

因此,我的问题是:* 我如何将存储的数据写入文件(并且仅是存储的数据,没有(La)TeX 命令或内部结构。* 我如何分配实际数据而不是宏?

梅威瑟:

\documentclass{beamer}
\usepackage{pgfkeys}
\usepackage{newfile}
\usepackage{pgfplots}

% source: http://tex.stackexchange.com/questions/37094
\newcommand{\setvalue}[1]{\pgfkeys{/variables/#1}}
\newcommand{\getvalue}[1]{\pgfkeysvalueof{/variables/#1}}
\newcommand{\declare}[1]{%
    \pgfkeys{
        /variables/#1.is family,
        /variables/#1.unknown/.style = {\pgfkeyscurrentpath/\pgfkeyscurrentname/.initial = ##1}
    }%
}

\declare{sec/}


\newinputstream{ccin} % create input stream
\IfFileExists{cleancolor.aux}{% only try to open file if it exists
\openinputfile{cleancolor.aux}{ccin}%
\setvalue{sec/number = \readaline{ccin}}%
%\foreach \x in {1,...,\getvalue{sec/number}} {%
%   \setvalue{sec/descr\x = \readaline{ccin}}%
%}%
\closeinputstream{ccin}%
}



\newcommand*{\ccdone}{%
    \newoutputstream{ccout}%
    \openoutputfile{cleancolor.aux}{ccout}%
    %\newcounter{ccoutnumber}%
    \addtostream{ccout}{4}%
    \foreach \x in {1,...,3} {
        \addtostream{ccout}{\getvalue{sec/descr\x}}%
    }
    \closeoutputstream{ccout}%
}

\begin{document}

\begin{frame}
%\getvalue{sec/number}
%\getvalue{sec/descr1}
%\getvalue{sec/descr2}
\end{frame}

\begin{frame}
\setvalue{sec/descr1 = Just}
\setvalue{sec/descr2 = {Some Simple}}
\setvalue{sec/descr3 = {Text}}
\getvalue{sec/descr1}
\getvalue{sec/descr2}
\getvalue{sec/descr3}
\end{frame}

\ccdone

\end{document}

cleandata.aux

4
\pgfk@/variables/sec/descr1 
\pgfk@/variables/sec/descr2 
\pgfk@/variables/sec/descr3 

更新 1

我想要做的事情如上所述,但有人问为什么。好吧,我的目标是定义一个新的 Beamer 主题。这个主题有点特殊,因为我想为各个部分添加描述,因为它们通常都是单词标题,需要一点说明。所以我做的是:

  1. 重新定义部分命令以添加描述
  2. 添加文件输入和输出以保存描述。
  3. 重新定义目录

目标是使下面 MWE 中的所有边都相同(并且等于最后一张幻灯片)。

\documentclass{beamer}
\usepackage{pgfkeys}
\usepackage{newfile}
\usepackage{pgfplots}
\usepackage{framed}

% source: http://tex.stackexchange.com/questions/37094
%\newcommand{\setvalue}[1]{\pgfkeys{/variables/#1}}
%\newcommand{\getvalue}[1]{\pgfkeysvalueof{/variables/#1}}
\newcommand{\declare}[1]{%
    \pgfkeys{
        /variables/#1.is family,
        /variables/#1.unknown/.style = {\pgfkeyscurrentpath/\pgfkeyscurrentname/.initial = ##1}
    }%
}

% declare key
\declare{sec/}

% read descriptions from file
\newinputstream{ccin} % create input stream
\IfFileExists{cleancolor.aux}{% only try to open file if it exists
\openinputfile{cleancolor.aux}{ccin}%
\pgfkeys{/variables/sec/number = \readaline{ccin}}%
%\foreach \x in {1,...,\getvalue{sec/number}} {%
%   \pgfkeys{/variables/sec/descr\x = \readaline{ccin}}%
%}%
\closeinputstream{ccin}%
}


% write descriptions to file
\newcommand*{\ccdone}{%
    \newoutputstream{ccout}%
    \openoutputfile{cleancolor.aux}{ccout}%
    %\newcounter{ccoutnumber}%
    \addtostream{ccout}{4}%
    \foreach \x in {1,...,3} {
        \addtostream{ccout}{\pgfkeysvalueof{/variables/sec/descr\x}}%
    }
    \closeoutputstream{ccout}%
}

% redefine table of contents
\defbeamertemplate*{section in toc}{}[1][]{
\begin{leftbar}
{\usebeamerfont{tocsection}\inserttocsection}\\ {\textit{\pgfkeysvalueof{/variables/sec/descr\inserttocsectionnumber}}}
\end{leftbar}
}


% redefine the section command
% 1st parameter: section name
% 2nd parameter: section description
\let\oldsection\section
\renewcommand{\section}[2]{\oldsection{#1}\pgfkeys{/variables/sec/descr\thesection = #2}}

\begin{document}

\frame{\tableofcontents}

\section{Technical set-up}{How it all comes together}
\frame{\tableofcontents}

\section{Functionality}{How will the end-user interact with the device?}
\frame{\tableofcontents}

\section{Results}{How accurate is location detection?}
\frame{\tableofcontents}

\ccdone

\end{document}

期望aux 文件应如下所示:

4
How it all comes together
How will the end-user interact with the device?
How accurate is location detection?

笔记:一些变量目前是硬编码的,这是为了使示例更加简单。

答案1

我改变了方法,我现在存储命令文件中的值并调用该文件。

要读取我使用的文件:

\makeatletter
\newread\ccinstream
\immediate\openin\ccinstream=list.tex
\@whilesw\unless\ifeof\ccinstream\fi{%
\immediate\read\ccinstream to \auxcommand%
\auxcommand%
}
\immediate\closein\ccinstream
\makeatother

并写入文件:

\newwrite\ccoutstream
\immediate\openout\ccoutstream=list.tex
\newcommand{\wtxt}[2]{
\immediate\write\ccoutstream{\noexpand\pgfkeys{/variables/sec/descr#1 = #2}}
}
\AtEndDocument{\immediate\closeout\ccoutstream}

相关内容