单独声明和放置列表、图表等。在标记列表中捕获和存储逐字内容

单独声明和放置列表、图表等。在标记列表中捕获和存储逐字内容

我正在尝试将图形、表格、列表等的定义和位置分开。我发现它们在编辑文档的散文时非常具有破坏性。它们经常占据段落集合中间的大量行,使得编辑文本变得困难。控制图形的位置通常需要将整个环境移到其他地方。

到目前为止,我已经能够通过调整给出的代码来使用图表和表格来实现这个答案。请参阅unplacedfigure下面的环境。它使用 xparse 捕获环境内容并将其存储在标记列表中。但是,\NewDocumentEnvironment无法捕获逐字内容。我该如何创建unplacedlisting环境?或者有什么好的替代方法吗?

这是我的代码:

\documentclass{article}

\usepackage{xparse}
\usepackage{listings}
\usepackage{lstautogobble}
\usepackage{graphicx}
\usepackage{float}

% An `unplaced` environment and a `place` command
% stolen from https://tex.stackexchange.com/a/561961/239612
\ExplSyntaxOn
\prop_new:N\g_unplaced_prop
\NewDocumentEnvironment{unplaced}{m +b}
  { \prop_gput:Nnn\g_unplaced_prop {#1} {#2}  }
  {}
\NewDocumentCommand{\place}{m}
  { \prop_item:Nn\g_unplaced_prop {#1}  }
\ExplSyntaxOff

% An `unplacedfigure` environment that specifies content of the listing
% separately from its placement. Notice that float specifiers are 
% given at placement time.
\NewDocumentEnvironment{unplacedfigure}{m +b}
  { \begin{unplaced}{#1} #2 }
  { \end{unplaced} }
\NewDocumentCommand{\placefigure}{m O{}}
  { \begin{figure}[#2]
      \place{#1}
    \end{figure}%
  }

% I want to define a similar environment for verbatim content.
\NewDocumentEnvironment{unplacedlisting}{m +b}
  { \begin{unplaced}{#1} #2 }
  { \end{unplaced} }
\NewDocumentCommand{\placelisting}{m}
  { TODO }

\begin{document}
\begin{unplacedfigure}{myfig1}
    \centering
    \caption{Two beautiful pictures.}
    \includegraphics[width=0.3\textwidth]{example-image-a}
    \includegraphics[width=0.2\textwidth]{example-image-b}
\end{unplacedfigure}
\begin{unplacedfigure}{myfig2}
    \centering
    \caption{Two pictures.}
    \includegraphics[width=0.15\textwidth]{example-image-a}
    \includegraphics[width=0.3\textwidth]{example-image-b}
\end{unplacedfigure}
% DOES NOT WORK:
% \begin{unplacedlisting}{mylisting}
%   code code code code
%   code special characters like % and }
%   code code code code
%   code code code code
%   code code code code
% \end{unplacedlisting}

text text text text text text text text text text
text text text text text text text text text text
text text text text text text text text text text
text text text text text etc.
%
\placefigure{myfig1}[H]
%
text text text text text etc.
%
\placefigure{myfig2}[H]
%
text text text text text etc.
%
\placelisting{mylisting}[hbp]
%
text text text text text etc.
%
\placefigure{myfig1}[H]

\end{document}

答案1

使用 great 包可以实现我们想要的功能scontents。我不得不稍微改变一下使用模式,因为似乎不可能scontents在自定义环境中包装环境。

存储图形的方式与以前完全相同,只是我们使用scontents环境而不是自定义unplacedfigure环境。

\begin{scontents}[store-env=myfig]
  <figure code>
\end{scontents}

要存储列表,我们需要在声明站点使用逐字环境,如下所示:

\begin{scontents}[store-env=mylst]
  \begin{verbatim}
   <verbatim content>
  \end{verbatim}
\end{scontents}

放置界面相同:

\placefigure{myfig}[htb!]
\placelisting{mylst}[H]

虽然它比原始建议稍微丑一些,但由于我们无法隐藏scontents实现细节,因此效果相当好。以下是完整代码:

\documentclass{article}

\usepackage{xparse}
\usepackage{listings}
\usepackage{lstautogobble}
\usepackage{graphicx}
\usepackage{float}
\usepackage{scontents}

\NewDocumentCommand{\placefigure}{m O{}}
  { \begin{figure}[#2]
      \getstored{#1}
    \end{figure}%
  }

\newfloat{listingfloat}{tbp}{asdf}[section]
\floatname{listingfloat}{Listing}
\NewDocumentCommand{\placelisting}{m O{}}
  { \begin{listingfloat}[#2]
      \getstored{#1}
    \end{listingfloat}%
  }

\begin{document}

\begin{scontents}[store-env=myfig1]
    \centering
    \caption{Two beautiful pictures.}
    \includegraphics[width=0.3\textwidth]{example-image-a}
    \includegraphics[width=0.2\textwidth]{example-image-b}
\end{scontents}
\begin{scontents}[store-env=myfig2]
    \centering
    \caption{Two pictures.}
    \includegraphics[width=0.15\textwidth]{example-image-a}
    \includegraphics[width=0.3\textwidth]{example-image-b}
\end{scontents}
\begin{scontents}[store-env=mylisting]
  \caption{A very advanced program}
  \begin{lstlisting}
  code code code code
  code special characters like % and }
  code code code code
  code code code code
  code code code code
  \end{lstlisting}
\end{scontents}

text text text text text text text text text text
text text text text text text text text text text
text text text text text text text text text text
text text text text text etc.
%
\placefigure{myfig1}[H]
%
text text text text text etc.
%
\placefigure{myfig2}[H]
%
text text text text text etc.
%
\placelisting{mylisting}[hbp]
%
text text text text text etc.
%
\placefigure{myfig1}[H]

\end{document}

相关内容