我的答案最后一个问题给出了将小命令挂接到 TeX 源中的换行符上的解决方案。
\documentclass{memoir}
\usepackage[spacing, tracking]{microtype}
\usepackage{libertine}
\usepackage{xcolor}
\usepackage{xspace}
\nonfrenchspacing
\microtypecontext{spacing=nonfrench}
\newcounter{srcline}
\newcommand{\linemark}{\stepcounter{srcline}%
\textcolor{red}{\textsc{Line~\thesrcline}}}
% Playing with the gobbling of \par
\makeatletter
{\obeylines\gdef\srclinepar{\@ifnextchar{^^M}{\endgraf\linemark\expandafter\srclinepar\@gobble}{ \linemark\xspace}}}
\makeatother
\newenvironment{srclines}
{\endgraf\let\par\srclinepar\obeylines}
{\endgraf}
\begin{document}
\begin{srclines}
Lorem ipsum dolor sit amet, consectetur adipiscing
elit. Quisque porttitor ullamcorper velit, eu
viverra augue varius in. Integer vehicula lacus
vitae nunc blandit, sit amet auctor ex volutpat.
Fusce ultrices elementum est, vel consectetur
nulla elementum vel. Praesent in elit eu odio
elementum luctus vel vitae leo. Sed vitae gravida
nunc. Sed id posuere ligula. Donec a odio
tincidunt, placerat nisl eget, egestas enim.
Vivamus eget massa in quam fermentum cursus. In
dictum, odio in laoreet fringilla, diam purus
suscipit lorem, sit amet tempus ex lectus eget
enim. Suspendisse potenti.%
\end{srclines}
\end{document}
我现在想做一些更复杂的事情。我不想扩展到一小段文本,而是想在每个源代码换行符处包含存储在另一个文件中的几个宏,例如使用\input{file.stuff}
(我使用同一目录中的空文件进行了测试)。
\documentclass{memoir}
\usepackage[spacing, tracking]{microtype}
\usepackage{libertine}
\usepackage{xcolor}
\usepackage{xspace}
\nonfrenchspacing
\microtypecontext{spacing=nonfrench}
\newcounter{srcline}
\newcommand{\linemark}{\stepcounter{srcline}%
\textcolor{red}{\textsc{Line~\thesrcline}}}
% Playing with the gobbling of \par
\makeatletter
{\obeylines\gdef\srclinepar{\@ifnextchar{^^M}{\endgraf\linemark\expandafter\srclinepar\@gobble}{ \linemark\xspace}\input{file.stuff}}}
% ^<----------------- here it is --------------
\makeatother
\newenvironment{srclines}
{\endgraf\let\par\srclinepar\obeylines}
{\endgraf}
\begin{document}
\begin{srclines}
Lorem ipsum dolor sit amet, consectetur adipiscing
elit. Quisque porttitor ullamcorper velit, eu
viverra augue varius in. Integer vehicula lacus
vitae nunc blandit, sit amet auctor ex volutpat.
Fusce ultrices elementum est, vel consectetur
nulla elementum vel. Praesent in elit eu odio
elementum luctus vel vitae leo. Sed vitae gravida
nunc. Sed id posuere ligula. Donec a odio
tincidunt, placerat nisl eget, egestas enim.
Vivamus eget massa in quam fermentum cursus. In
dictum, odio in laoreet fringilla, diam purus
suscipit lorem, sit amet tempus ex lectus eget
enim. Suspendisse potenti.%
\end{srclines}
\end{document}
但这会产生错误:
TeX 容量超出,抱歉 [文本输入级别=15]。^^M
有没有办法在每个换行符处输入一个外部文件?
答案1
像这样?你加\input
错地方了,里面 \linemark
就在它应该在的地方。
\documentclass{memoir}
\usepackage[spacing, tracking]{microtype}
\usepackage{libertine}
\usepackage{xcolor}
\usepackage{xspace}
\nonfrenchspacing
\microtypecontext{spacing=nonfrench}
\newcounter{srcline}
\newcommand{\linemark}{\stepcounter{srcline}%
\textcolor{red}{\textsc{Line~\thesrcline}} \input{file}}
% Playing with the gobbling of \par
\makeatletter
{\obeylines\gdef\srclinepar{\@ifnextchar{^^M}{\endgraf\linemark\expandafter\srclinepar\@gobble}{ \linemark\xspace}}}
% ^<----------------- here it is --------------
\makeatother
\begin{filecontents*}{file.tex}
\textcolor{teal}{\bfseries!?}%
\end{filecontents*}
\newenvironment{srclines}
{\endgraf\let\par\srclinepar\obeylines}
{\endgraf}
\begin{document}
\begin{srclines}
Lorem ipsum dolor sit amet, consectetur adipiscing
elit. Quisque porttitor ullamcorper velit, eu
viverra augue varius in. Integer vehicula lacus
vitae nunc blandit, sit amet auctor ex volutpat.
Fusce ultrices elementum est, vel consectetur
nulla elementum vel. Praesent in elit eu odio
elementum luctus vel vitae leo. Sed vitae gravida
nunc. Sed id posuere ligula. Donec a odio
tincidunt, placerat nisl eget, egestas enim.
Vivamus eget massa in quam fermentum cursus. In
dictum, odio in laoreet fringilla, diam purus
suscipit lorem, sit amet tempus ex lectus eget
enim. Suspendisse potenti.%
\end{srclines}
\end{document}
该filecontents
环境只是为了使示例自成一体。