不使用 % 和注释环境来注释行

不使用 % 和注释环境来注释行

我想问一些能极大地方便我工作的问题。但在此之前,我先介绍一下我面临的问题。为了收集我的笔记给我的学生,并给他们提供像书一样更优质的东西,我创建了数百个练习,每个练习都是一个小的自己的文件。我通过输入它们\input{...}

我面临的问题是,每个单独文件的前 2 或 3 行都有某些信息,例如练习来源、练习所在文件的名称等。我不希望这些信息出现在最终结果中,因此我用 注释掉它们%。然而,偶尔,当我仍在处理文本时,我希望出现这些私人信息我在每个单独的文件的第一行都注释了。你可以想象打开 500 多个文件,注释掉%第一行和第二行,然后反过来做会有些痛苦。


这是我的问题:Latex 是否有内部方法来读取导入的文件,但忽略前两行或三行尚未注释掉的内容%?当然,这可以通过在第一行前面添加脚本从命令行完成%(我在 Linux 上工作),但使用一些内部切换到 LaΤeΧ 非常优雅且容易。所以我可以选择何时显示第一行,何时不显示。


一个例子我如何组织每个练习

\\ {\color{red}{\small is the  exercise.435.tex}}  % is the name of the file
\\ {\color{red}{\small 1.84 ex. 127 page notes}}  % is the source of exercise etc

\begin{exercise}    
. . . 
\end{exercise}

最后 在您的帮助下,经过两天的时间。

我在这里收集了我最终使用的最终形式。我添加了检测练习名称的自动化功能。也许像我一样的新用户会发现一些有用的东西

我在这里写是因为我不知道在哪里写下这些想法。如果有其他选择,请告诉我

\documentclass[a4paper,10pt]{article}
\usepackage{xcolor}

\usepackage[realmainfile]{currfile}[2012/05/06] % detects the names of imported files

%%%%%%%%%%%%%%%%%%%%%%%%%% for prologue ( The code is from the @egreg ) %%%%%%%%%%%%%%%%%%%%%%%%%%%%
\newif\ifprolog  
\long\def\startprolog#1\stopprolog{%
  \ifprolog
    \par
    \begingroup
    \let\\\par
    \color{red}\small \\  \currfilepath \\ #1    % \currfilepath for appear the name of each exersice is my extra  addition 
    \par\medskip                          
    \endgroup                                 
  \fi}

\prologtrue % for to display the prologue ( comment out to disappear )

%%%%%%%%%%%%%%%%%%%%%%%%%% definition of exercise %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\newtheorem{exercise}{\color{magenta} \Large \bfseries} % [chapter]    %[section]   %% at the book documentclass 


\begin{document}

 text above the exercise

 \input{exersice.435.tex}

 Text below the exercise

 \end{document}

外部文件exersice.435.tex的形式如下:

\startprolog
  1.84 ex. 127 page notes   % is the source of exercise etc
\stopprolog

\begin{exercise}
 Here is the text of the exercise
\end{exercise}

最终结果

答案1

如果您的输入文件都具有相同的形式,即一些行以 开头\\,后面跟着一个空行,您可以通过临时重新定义\\和更改输入练习的命令来实现:

\newcommand{\exeinput}[1]{%
  \let\latexdoublebackslash\\
  \ifprolog\let\\\showprolog\else\let\\\hideprolog\fi
  \input{#1}}

\def\showprolog#1\par{%
  \def\\{\par\noindent}% redefine `\\` to end lines
  \par\noindent
  #1% print the lines
  \par % end the last line
  \let\\\latexdoublebackslash}

\def\hideprolog#1\par{\let\\\latexdoublebackslash} % throw away the prolog lines

\newif\ifprolog

所以你可以说

\exeinput{exercise.435.tex}

如果您设置了,\prologtrue那么序言行也将被打印。

然而,如果序言和主要内容之间没有空行,这种方法就会失败,因此特殊标记可能是最佳策略。例如,如果你的练习文件具有以下形式

\startprolog
\\ is the  exercise.435.tex % is the name of the file
\\ 1.84 ex. 127 page notes  % is the source of exercise etc
\stopprolog

\begin{exercise}    
. . . 
\end{exercise}

你可以说

\newif\ifprolog
\long\def\startprolog#1\stopprolog{%
  \ifprolog
    \par
    \begingroup
    \let\\\par
    \color{red}\small #1
    \par\medskip
    \endgroup
  \fi}

所以

\input{exercise.435.tex}

除非您设置 ,否则将打印不带序言的练习\prologtrue。优点是,如果序言与内容不分离,则不会有任何风险,并且当您想要打印它时可以自由地重新格式化序言(例如,可能作为边注)。

答案2

LaTeX 是一种标记语言。对于私人信息,可以使用宏。文件exercise.435.tex可能看起来像:

\private{is the \filename{exercise.435.tex}}
\private{1.84 ex.\@ 127 page notes}

\begin{exercise}
...
\end{exercise}

然后\private可以定义宏来将内容设置为红色和小字体:

\newcommand*{\private}[1]{%
  \ifhmode\newline\fi
  \textcolor{red}{\small #1}%
  \ignorespaces
}%
\newcommand*{\filename}[1]{#1}% or \texttt{#1} or ...

并且很容易抑制这些线条:

\newcommand*{\private}[1]{\ignorespaces}

\ignorespaces删除行尾\private{...}以避免不需要的空格。

答案3

您可以反过来,更改 % 的 catcode,使其在您开始读取文件时不再充当注释字符。然后练习环境可以恢复更改。您还可以使用其他(未使用的)字符作为特殊注释符号。但这种 catcode 更改有点脆弱,因此这个想法是否可用在很大程度上取决于您的文档:

\documentclass{article}
\begin{document}

\catcode`\%=9
% not ignored
% not ignored

\catcode`\%=13
\def%{\par Private: }
% not ignored
% not ignored

\catcode`\%=14
% ignored
% ignored
blub

\catcode`\@=14
@ ignored
@ ignored
exercise

\end{document}

答案4

(考虑到之前的答案的质量,我对发布另一个答案有点紧张......)

这个是通过查看你想要的保持而不是你想扔掉的东西。它假设你想要的是介于 之间的内容\begin{exercise} ... \end{exercise}。它还假设你不会在其中使用 catcode 做任何花哨的事情,因此可以安全地将其放入宏主体中(使用environ我们将练习文件装入一个盒子中,然后将其扔掉,这样可以处理掉任何额外的垃圾,同时保留练习的内容以供之后立即重复使用。

显然,我不得不构建一些示例代码,因此这可能需要根据您的具体情况进行调整。我认为这样做的好处是您不需要更改现有文件,而且它的格式非常可靠:环境之外的所有内容都会被exercise扔进垃圾箱。但是,如果它们执行诸如全局分配之类的操作,它就不可靠了,但我认为我可以相当安全地假设它们没有这样做。

\documentclass{article}


\usepackage{xcolor}
\usepackage{filecontents}
\usepackage{environ}

\begin{filecontents*}{onlyexaux.tex}
\\ {\color{red}{\small is the  exercise.435.tex}}  % is the name of the file
\\ {\color{red}{\small 1.84 ex. 127 page notes}}  % is the source of exercise etc

\begin{exercise}    
This is an exercise.
You should do it carefully.
\end{exercise}
\end{filecontents*}

\newcounter{exercise}
\newenvironment{exercise}{%
  \begin{minipage}{.5\textwidth}%
  \refstepcounter{exercise}%
  \noindent\textbf{Exercise~\theexercise}\par}{\end{minipage}}

\newcommand{\getexercise}[1]{~\input{#1}}

\newif\ifprivate
\privatetrue
\ifprivate
\else

\let\oexercise=\exercise
\let\endoexercise=\endexercise
\let\exercise=\relax
\let\endexercise=\relax
\newbox\exbox
\NewEnviron{exercise}{%
  \global\let\currentexercise=\BODY
}

\renewcommand{\getexercise}[1]{%
  \setbox\exbox=\hbox\bgroup \input{#1}\egroup
  \begin{oexercise}%
  \currentexercise%
  \end{oexercise}%
}

\fi
\begin{document}

\getexercise{onlyexaux}

\end{document}

目前,开关是\privatetrue主文件中的一行。显然,它可以隐藏得更整齐一些。

相关内容