就像是:
如果 下一个标记是一些人为创建分页符的标记(例如
\newpage
,\clearpage
然后 在它之后注入一些东西
别的 在它之前注入一些东西
我想修改\input
以在 PDF 中提供视觉提示。当我的文档\input
以分页符开头时,会出现“问题”。它不是真的有问题,因为我的注入总是出现在我期望的位置。我只是希望我的注入显示在新页面的开头。
定义注射
在这种情况下,有两个 TikZ 覆盖,但它可能是字母“b”。
% TikZ Overlay
\makeatletter
\newcounter{mac@input-tracker}% for unique tikz coordinate/node labels
\tikzstyle{inputtext}=[xshift=2mm,orange,font=\tiny]
\def\tikzbegininput{% Injection before input
\tikz [remember picture] \coordinate (input-tracker-\the\value{mac@input-tracker});
\begin{tikzpicture}[remember picture,overlay]
\node [anchor=south west,inputtext] (input-tracker-text-\the\value{mac@input-tracker}) at
(input-tracker-\the\value{mac@input-tracker} -| current page.west) {Begin \detokenize\expandafter{\tmp}};
\draw [thin,densely dotted,orange] (input-tracker-text-\the\value{mac@input-tracker}.east) --
(current page.east |- input-tracker-text-\the\value{mac@input-tracker});
\draw [thin,densely dotted,orange,xshift=5mm,->] (input-tracker-text-\the\value{mac@input-tracker}.south west) --
($(input-tracker-text-\the\value{mac@input-tracker}.west)+(0,-5mm)$);
\draw [line width=2mm,orange] (input-tracker-text-\the\value{mac@input-tracker}.west) --
(current page.west |- input-tracker-text-\the\value{mac@input-tracker}.west);
\end{tikzpicture}
}%
\def\tikzendinput{% Injection after input
\tikz [remember picture] \coordinate (input-tracker-\the\value{mac@input-tracker}-end);
\begin{tikzpicture}[remember picture, overlay]
\node [anchor=north west,inputtext] (input-tracker-text-\the\value{mac@input-tracker}-end) at
(input-tracker-\the\value{mac@input-tracker}-end -| current page.west) {End \detokenize\expandafter{\tmp}};
\draw [thin,densely dotted,orange] (input-tracker-text-\the\value{mac@input-tracker}-end.east) -- (current page.east |- input-tracker-text-\the\value{mac@input-tracker}-end);
\draw [thin,densely dotted,orange,xshift=5mm,->] (input-tracker-text-\the\value{mac@input-tracker}-end.north west) -- ($(input-tracker-text-\the\value{mac@input-tracker}-end.west)+(0,5mm)$);
\end{tikzpicture}
}%
\makeatother
重新定义输入以包含注入
% Redefine input to accept TikZ overlay injection
\makeatletter
\AtBeginDocument{% Redefine \input
\let\latex@input\input%
\newcommand\red@input[1]{%
\stepcounter{mac@input-tracker}% add one to unique tikz label counter
\protected@edef\tmp{#1}
\tikzbegininput % inject tikz overlay at begin input
\IfFileExists{#1}{% Added some failsafe logic so doc still compiles when input missing (usually not good unless you enjoy reading logs)
\latex@input{#1}% actual input
}{}
{% Does not exist
\typeout{RegexFindableErrorPrefix: Missing file "#1"}% for error report (also allows document to compile, TikZ overlays make missing file visible in PDF
}
\tikzendinput % inject tikz overlay at end input
}
\let\input\red@input% Set \input to new definition
}%
\makeatother
外部文件内容
lipsum.tex
\newpage\lipsum[1]
整合在一起
\documentclass{article}
\usepackage{lipsum}% dummy text from elegant Latin
\usepackage{tikz}
\usetikzlibrary{calc}
% Redefine input to accept TikZ overlay injection
\makeatletter
\AtBeginDocument{% Redefine \input
\let\latex@input\input%
\newcommand\red@input[1]{%
\stepcounter{mac@input-tracker}% add one to unique tikz label counter
\protected@edef\tmp{#1}
\tikzbegininput % inject tikz overlay at begin input
\IfFileExists{#1}{% Added some failsafe logic so doc still compiles when input missing (usually not good unless you enjoy reading logs)
\latex@input{#1}% actual input
}{}
{% Does not exist
\typeout{RegexFindableErrorPrefix: Missing file "#1"}% for error report (also allows document to compile, TikZ overlays make missing file visible in PDF
}
\tikzendinput % inject tikz overlay at end input
}
\let\input\red@input% Set \input to new definition
}%
\makeatother
% TikZ Overlay
\makeatletter
\newcounter{mac@input-tracker}% for unique tikz coordinate/node labels
\tikzstyle{inputtext}=[xshift=2mm,orange,font=\tiny]
\def\tikzbegininput{% Injection before input
\tikz [remember picture] \coordinate (input-tracker-\the\value{mac@input-tracker});
\begin{tikzpicture}[remember picture,overlay]
\node [anchor=south west,inputtext] (input-tracker-text-\the\value{mac@input-tracker}) at
(input-tracker-\the\value{mac@input-tracker} -| current page.west) {Begin \detokenize\expandafter{\tmp}};
\draw [thin,densely dotted,orange] (input-tracker-text-\the\value{mac@input-tracker}.east) --
(current page.east |- input-tracker-text-\the\value{mac@input-tracker});
\draw [thin,densely dotted,orange,xshift=5mm,->] (input-tracker-text-\the\value{mac@input-tracker}.south west) --
($(input-tracker-text-\the\value{mac@input-tracker}.west)+(0,-5mm)$);
\draw [line width=2mm,orange] (input-tracker-text-\the\value{mac@input-tracker}.west) --
(current page.west |- input-tracker-text-\the\value{mac@input-tracker}.west);
\end{tikzpicture}
}%
\def\tikzendinput{% Injection after input
\tikz [remember picture] \coordinate (input-tracker-\the\value{mac@input-tracker}-end);
\begin{tikzpicture}[remember picture, overlay]
\node [anchor=north west,inputtext] (input-tracker-text-\the\value{mac@input-tracker}-end) at
(input-tracker-\the\value{mac@input-tracker}-end -| current page.west) {End \detokenize\expandafter{\tmp}};
\draw [thin,densely dotted,orange] (input-tracker-text-\the\value{mac@input-tracker}-end.east) -- (current page.east |- input-tracker-text-\the\value{mac@input-tracker}-end);
\draw [thin,densely dotted,orange,xshift=5mm,->] (input-tracker-text-\the\value{mac@input-tracker}-end.north west) -- ($(input-tracker-text-\the\value{mac@input-tracker}-end.west)+(0,5mm)$);
\end{tikzpicture}
}%
\makeatother
\begin{document}
\lipsum[1]
\input{lipsum.tex}
\lipsum[1]
\end{document}
输出
此输出无需\newpage
在 lipsum.tex 中即可创建紧凑的插图。
也许有办法和\futurelet
一起\afterassignment
?
答案1
这是一个解决方案。我们重新定义命令\InputIfFileExists
\AtBeginDocument{%
\long\def \InputIfFileExists#1#2{%
\stepcounter{mac@input-tracker}%
\protected@edef\tmp{#1}%
\IfFileExists{#1}%
{#2\@addtofilelist{#1}\expandafter\mtt\@@input \@filef@und \tikzendinput}}}
改变\@@input \@filef@und -->\expandafter\mtt\@@input \@filef@und \tikzendinput
新命令\mtt
使用以下方式定义字符串包裹
\newcommand{\mtt}[1]{%
\IfStrEqCase{#1}{%
{\newpage}{%
\let\mtnewpage\newpage
\newpage
\def\newpage{\let\newpage\mtnewpage}\tikzbegininput}%
{\clearpage}{\let\mtclearpage\clearpage
\clearpage
\def\clearpage{\let\clearpage\mtclearpage}\tikzbegininput}}%
[\tikzbegininput#1]}
完整代码
\documentclass{article}
\usepackage{lipsum}% dummy text from elegant Latin
\usepackage{tikz}
\usetikzlibrary{calc}
\usepackage{xstring}
\normalexpandarg
\makeatletter
\newcommand{\mtt}[1]{%
\IfStrEqCase{#1}{%
{\newpage}{%
\let\mtnewpage\newpage
\newpage
\def\newpage{\let\newpage\mtnewpage}\tikzbegininput}%
{\clearpage}{\let\mtclearpage\clearpage
\clearpage
\def\clearpage{\let\clearpage\mtclearpage}\tikzbegininput}}%
[\tikzbegininput#1]}
\AtBeginDocument{%
\long\def \InputIfFileExists#1#2{%
\stepcounter{mac@input-tracker}%
\protected@edef\tmp{#1}%
\IfFileExists{#1}%
{#2\@addtofilelist{#1}\expandafter\mtt\@@input \@filef@und \tikzendinput}}}
% TikZ Overlay
\newcounter{mac@input-tracker}% for unique tikz coordinate/node labels
\tikzstyle{inputtext}=[xshift=2mm,orange,font=\tiny]
\def\tikzbegininput{% Injection before input
\tikz [remember picture] \coordinate (input-tracker-\the\value{mac@input-tracker});
\begin{tikzpicture}[remember picture,overlay]
\node [anchor=south west,inputtext] (input-tracker-text-\the\value{mac@input-tracker}) at
(input-tracker-\the\value{mac@input-tracker} -| current page.west) {Begin \detokenize\expandafter{\tmp}};
\draw [thin,densely dotted,orange] (input-tracker-text-\the\value{mac@input-tracker}.east) --
(current page.east |- input-tracker-text-\the\value{mac@input-tracker});
\draw [thin,densely dotted,orange,xshift=5mm,->] (input-tracker-text-\the\value{mac@input-tracker}.south west) --
($(input-tracker-text-\the\value{mac@input-tracker}.west)+(0,-5mm)$);
\draw [line width=2mm,orange] (input-tracker-text-\the\value{mac@input-tracker}.west) --
(current page.west |- input-tracker-text-\the\value{mac@input-tracker}.west);
\end{tikzpicture}
}%
\def\tikzendinput{% Injection after input
\tikz [remember picture] \coordinate (input-tracker-\the\value{mac@input-tracker}-end);
\begin{tikzpicture}[remember picture, overlay]
\node [anchor=north west,inputtext] (input-tracker-text-\the\value{mac@input-tracker}-end) at
(input-tracker-\the\value{mac@input-tracker}-end -| current page.west) {End \detokenize\expandafter{\tmp}};
\draw [thin,densely dotted,orange] (input-tracker-text-\the\value{mac@input-tracker}-end.east) -- (current page.east |- input-tracker-text-\the\value{mac@input-tracker}-end);
\draw [thin,densely dotted,orange,xshift=5mm,->] (input-tracker-text-\the\value{mac@input-tracker}-end.north west) -- ($(input-tracker-text-\the\value{mac@input-tracker}-end.west)+(0,5mm)$);
\end{tikzpicture}
}%
\makeatother
\begin{document}
\lipsum[1]
\input{lipsum}
\lipsum[1]
\end{document}
更新:解释
\IfStrEqCase{string}{%
{string1}{excute code 1}%
{string2}{excute code 2}%
{string3}{excute code 3}%
...
{stringN}{excute code N}%
[〈other cases code〉]
依次测试 〈string〉 是否等于 〈string1〉、〈string2〉等。与 进行比较
\IfStrEq
(见上文)。如果测试编号 i 为正(〈string〉 与 〈string i〉 匹配),则宏运行 〈code i〉并结束。如果所有测试均失败,则宏运行可选的 〈other cases code〉(如果存在)。
在我们的例子中string=#1
是输入文件中的第一个标记,,,string1
...... string2
是\newpage
,\clearpage
...