将内容附加到包含 sharp 的文件

将内容附加到包含 sharp 的文件

我的图书馆,我需要能够将一些文本附加到文件中。问题是该文件可能包含符号#,事实证明,每次我将某些内容附加到包含的文件中时#,每个#都会变成两个尖角:##,所以我的库丢失了,因为与(问题)\#确实不同\##这里)。

有没有办法将内容附加到包含的文件#?目前我正在使用此命令

\NewDocumentCommand\appendtofile{m+m}{%
  \begingroup
  \IfFileExists{#1}%
  {\CatchFileDef{\filecontent}{#1}{\catcode`\\=12 \endlinechar=`^^J\catcode\endlinechar=12\relax}}% keep existing end-of-lines
  {\let\filecontent\empty}%
  \immediate\openout\appendwrite=#1\relax
  \immediate\write\appendwrite{\detokenize\expandafter{\filecontent}#2}%
  \immediate\closeout\appendwrite
  \endgroup
}

平均能量损失

\documentclass{article}
\usepackage{catchfile} % To append to a file.
\usepackage{etoolbox}

\makeatletter
\newwrite\appendwrite

%%% This command allows the user to append things to a file. But the content may contain macros that
%%% will be evaluated before writing them to the file.
% The first argument is the file name
% The second argument is the text to write
% https://tex.stackexchange.com/questions/11796/how-can-i-open-a-file-in-append-mode
\NewDocumentCommand\appendtofile{m+m}{%
  \begingroup
  \IfFileExists{#1}%
  {\CatchFileDef{\filecontent}{#1}{\catcode`\\=12 \endlinechar=`^^J\catcode\endlinechar=12\relax}}% keep existing end-of-lines
  {\let\filecontent\empty}%
  \immediate\openout\appendwrite=#1\relax
  \immediate\write\appendwrite{\detokenize\expandafter{\filecontent}#2}%
  \immediate\closeout\appendwrite
  \endgroup
}

%%% Add some rules and title before and after the text.
%% The first argument is the name of the file,
%% The second argument is the text to add.
\NewDocumentCommand\appendEnclosed{m+m}{
  \appendtofile{#1}{% This part may contain macros, potentially expanded like \myTitle:
    \string\par\string\noindent\string\rule{\string\linewidth}{2mm}\par%
    \string\begin{center}\myTitle\string\end{center}
    \string\par\string\noindent\string\rule{\string\linewidth}{2mm}\par%
    \detokenize{#2}%%<-- this part should be printed as closely as possible to the original text, including # and if possible new lines.
    \string\par\string\noindent\string\rule{\string\linewidth}{.5mm}\par%
  }
}

%% Erase the file at the beginning to avoid accumulating things from previous runs.
\immediate\openout\appendwrite=mynewfile.tex%
\immediate\write\appendwrite{}%
\immediate\closeout\appendwrite%

\makeatother

\begin{document}
\def\myTitle{myTitle}

\appendEnclosed{mynewfile}{
  Hello

  I would love to preserve lines, but if it's not possible I can live without if latex at least adds par. I would like to be able to add macros like \textbf{a bold one}.
}

\appendEnclosed{mynewfile}{
  But what I would love even more is the ability to add sharp like \#. This work if I write a single sharp, but every time I append something to the file, each sharp is replaced with two sharps. Try to uncomment the next lines to see by yourself.
}
% \appendEnclosed{mynewfile}{
%   The number of sharp depends on the number of time we happened something to the file. I guess when the file is read, each sharp is turned into two sharps.
% }
% \appendEnclosed{mynewfile}{
%   The number of sharp depends on the number of time we happened something to the file. I guess when the file is read, each sharp is turned into two sharps.
% }
% \appendEnclosed{mynewfile}{
%   The number of sharp depends on the number of time we happened something to the file. I guess when the file is read, each sharp is turned into two sharps.
% }


\section{Content of the file}
\def\myTitle{} % Just to make sure that myTitle was properly expanded when writing to the file.
\def\mymacro{MyMacro}
\input{mynewfile}


\end{document}

编辑 我刚刚意识到这里提出的答案https://tex.stackexchange.com/a/13289/116348没有同样的问题。我设法将其调整到适合我的用例。如果我遗漏了一些微妙的极端情况,请告诉我。

\documentclass{article}
\usepackage{catchfile} % To append to a file.
\usepackage{etoolbox}

\makeatletter

%% https://tex.stackexchange.com/a/13289/116348
\newwrite\appendwrite
\newcount\pratend@count@makeallother

%%% Loop to make sure all chars are normal letters, including #.
\newcommand{\makeallother}{%
  \pratend@count@makeallother0\relax
  \loop\ifnum\pratend@count@makeallother<255\relax
  \catcode\pratend@count@makeallother12\relax
  \advance\pratend@count@makeallother by 1\relax
  \repeat
}

%%% This command allows the user to append things to a file. But the content may contain macros that
%%% will be evaluated before writing them to the file.
%%% TODO: Efficiency may be improved by writing to the file only before the printProof.
% The first argument is the file name
% The second argument is the text to write
\NewDocumentCommand\appendtofile{m+m}{%
  \begingroup
  %% Read the content
  \begingroup%
  \IfFileExists{#1}{%
    \newlinechar\endlinechar%
    \makeallother% Turn all chars into normal letters.
    \everyeof{\noexpand}%
    \edef\fileContent{\@@input #1 }%
  }{%
    \let\fileContent\empty
  }%
  %% Open the file to write in it:
  \immediate\openout\appendwrite #1\relax%
  %% Write the text
  \immediate\write\appendwrite{\fileContent}%
  \endgroup%
  \immediate\write\appendwrite{#2}%
  %% Close the file
  \immediate\closeout\appendwrite%
  \endgroup
}

\NewDocumentCommand\appendEnclosed{m+m}{
  \appendtofile{#1}{% This part may contain macros, potentially expanded like \myTitle:
    \string\par\string\noindent\string\rule{\string\linewidth}{2mm}\par%
    \string\begin{center}\myTitle\string\end{center}
    \string\par\string\noindent\string\rule{\string\linewidth}{2mm}\par%
    \detokenize{#2}%%<-- this part should be printed as closely as possible to the original text, including # and if possible new lines.
    \string\par\string\noindent\string\rule{\string\linewidth}{.5mm}\par%
  }
}

%% Erase the file at the beginning to avoid accumulating things from previous runs.
\immediate\openout\appendwrite=mynewfile.tex%
\immediate\write\appendwrite{}%
\immediate\closeout\appendwrite%

\makeatother

\begin{document}
\def\myTitle{myTitle}

\appendEnclosed{mynewfile.tex}{
  Hello

  I would love to preserve lines, but if it's not possible I can live without if latex at least adds par. I would like to be able to add macros like \textbf{a bold one}.
}

\appendEnclosed{mynewfile.tex}{
  But what I would love even more is the ability to add sharp like this: \#. This work if I write a single sharp, but every time I append something to the file, each sharp is replaced with two sharps. Try to uncomment the next lines to see by yourself.
}

\appendEnclosed{mynewfile.tex}{
  3333
}


% \appendEnclosed{mynewfile}{
%   The number of sharp depends on the number of time we happened something to the file. I guess when the file is read, each sharp is turned into two sharps.
% }
% \appendEnclosed{mynewfile}{
%   The number of sharp depends on the number of time we happened something to the file. I guess when the file is read, each sharp is turned into two sharps.
% }
% \appendEnclosed{mynewfile}{
%   The number of sharp depends on the number of time we happened something to the file. I guess when the file is read, each sharp is turned into two sharps.
% }


\section{Content of the file}
\def\myTitle{} % Just to make sure that myTitle was properly expanded when writing to the file.
\def\mymacro{MyMacro}
\input{mynewfile}


\end{document}

答案1

我终于根据这个答案解决了这个问题https://tex.stackexchange.com/a/13289/116348,我不确定这是否是最好的解决方案,但到目前为止它似乎有效^^

\documentclass{article}
\usepackage{catchfile} % To append to a file.
\usepackage{etoolbox}

\makeatletter

%% https://tex.stackexchange.com/a/13289/116348
\newwrite\appendwrite
\newcount\pratend@count@makeallother

%%% Loop to make sure all chars are normal letters, including #.
\newcommand{\makeallother}{%
  \pratend@count@makeallother0\relax
  \loop\ifnum\pratend@count@makeallother<255\relax
  \catcode\pratend@count@makeallother12\relax
  \advance\pratend@count@makeallother by 1\relax
  \repeat
}

%%% This command allows the user to append things to a file. But the content may contain macros that
%%% will be evaluated before writing them to the file.
%%% TODO: Efficiency may be improved by writing to the file only before the printProof.
% The first argument is the file name
% The second argument is the text to write
\NewDocumentCommand\appendtofile{m+m}{%
  \begingroup
  %% Read the content
  \begingroup%
  \IfFileExists{#1}{%
    \newlinechar\endlinechar%
    \makeallother% Turn all chars into normal letters.
    \everyeof{\noexpand}%
    \edef\fileContent{\@@input #1 }%
  }{%
    \let\fileContent\empty
  }%
  %% Open the file to write in it:
  \immediate\openout\appendwrite #1\relax%
  %% Write the text
  \immediate\write\appendwrite{\fileContent}%
  \endgroup%
  \immediate\write\appendwrite{#2}%
  %% Close the file
  \immediate\closeout\appendwrite%
  \endgroup
}

\NewDocumentCommand\appendEnclosed{m+m}{
  \appendtofile{#1}{% This part may contain macros, potentially expanded like \myTitle:
    \string\par\string\noindent\string\rule{\string\linewidth}{2mm}\par%
    \string\begin{center}\myTitle\string\end{center}
    \string\par\string\noindent\string\rule{\string\linewidth}{2mm}\par%
    \detokenize{#2}%%<-- this part should be printed as closely as possible to the original text, including # and if possible new lines.
    \string\par\string\noindent\string\rule{\string\linewidth}{.5mm}\par%
  }
}

%% Erase the file at the beginning to avoid accumulating things from previous runs.
\immediate\openout\appendwrite=mynewfile.tex%
\immediate\write\appendwrite{}%
\immediate\closeout\appendwrite%

\makeatother

\begin{document}
\def\myTitle{myTitle}

\appendEnclosed{mynewfile.tex}{
  Hello

  I would love to preserve lines, but if it's not possible I can live without if latex at least adds par. I would like to be able to add macros like \textbf{a bold one}.
}

\appendEnclosed{mynewfile.tex}{
  But what I would love even more is the ability to add sharp like this: \#. This work if I write a single sharp, but every time I append something to the file, each sharp is replaced with two sharps. Try to uncomment the next lines to see by yourself.
}

\appendEnclosed{mynewfile.tex}{
  3333
}


% \appendEnclosed{mynewfile}{
%   The number of sharp depends on the number of time we happened something to the file. I guess when the file is read, each sharp is turned into two sharps.
% }
% \appendEnclosed{mynewfile}{
%   The number of sharp depends on the number of time we happened something to the file. I guess when the file is read, each sharp is turned into two sharps.
% }
% \appendEnclosed{mynewfile}{
%   The number of sharp depends on the number of time we happened something to the file. I guess when the file is read, each sharp is turned into two sharps.
% }


\section{Content of the file}
\def\myTitle{} % Just to make sure that myTitle was properly expanded when writing to the file.
\def\mymacro{MyMacro}
\input{mynewfile}


\end{document}

相关内容