如何将列表的标题放在边缘?(scrreprt,floatrow)

如何将列表的标题放在边缘?(scrreprt,floatrow)

在下面的 mwe 中,我使用floatrow包将图形标题放在边缘。

我怎样才能做同样的事情listings

更新:

有些列表分布在两页上。

\documentclass[a4paper, 10pt]{scrreprt}

\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}

\usepackage{mwe}
\usepackage{lipsum}
\usepackage{todonotes}

\usepackage[top=1cm, bottom=1cm, left=2cm, right=5cm]{geometry}
\usepackage{txfonts}
\usepackage{xcolor}

\usepackage{listings}
\lstset{
    language=C,
    basicstyle=\ttfamily,
    keywordstyle=\bfseries,
    backgroundcolor=\color{black!10}}

\usepackage{caption}
\captionsetup{format=plain, font={small, it}, labelfont={bf}}

\usepackage{floatrow}
\floatsetup[figure]{
    margins=hangright,
    capposition=beside,
    capbesideposition={top,right},
    floatwidth=\textwidth}

\begin{document}

\chapter{How to place the caption of a lstlisting in the margin?}
\blindtext

\begin{figure}[h]
  \includegraphics[width=\textwidth, height=5cm]{example-image-a}
  \caption{Greybox with an 'A' in the center}
\end{figure}

\blindtext

\todo[]{TODO: Place the caption of the following listing in the margin.}
\begin{lstlisting}[caption={\lstinline{main()} function}]
int main(void)
{
    while (true) {
        /* loop forever */
    }
}
\end{lstlisting}%

\blindtext

\end{document}

浮排 mwe

答案1

我猜是\usepackage{caption}因为\hangindent不是零。它将scrreprt边距段落与对齐:。但由于caption与我的代码冲突我不会用它我用新的方法更新了我的答案。

  • 声明一个新的浮点类型mylst。现在你可以用来floatrow格式化字幕。
  • 重新定义,\lst@makecaption这样listings就不会再排版标题了。相反,我将标题文本传递给 a mylst。(换句话说,我想mylst在你的 C 代码前面放一个。但我不能这样做,直到lstlisting读到你的标题文本。)
  • 注意\hangindent和的间距。(请注意\vskip-1\baselineskip\vskip-2\baselineskip是神奇的数字。间距\documentclass取决于。)

\documentclass[a4paper,10pt]{scrreprt}
\usepackage{listings}
\usepackage[top=1cm,bottom=1cm,left=2cm,right=5cm]{geometry}
\usepackage{floatrow}
    \DeclareNewFloatType{mylst}{placement=h!,name=Mylst,fileext=lom}
    \floatsetup{margins=hangright,floatwidth=\textwidth,capposition=beside,capbesideposition={top,right}}

\begin{document}
    \listof{mylst}{List of Mylst}
    \hrule
    \begin{figure}[h!]
        \caption{usual figure caption}
        so sad you cannot see the figure.
    \end{figure}
    \hrule
    \begin{lstlisting}
        \This#is$the&usual^lstlisting_.
    \end{lstlisting}
    \hrule
    \makeatletter
    \def\lst@makecaption#1#2{
        \vskip-1\baselineskip\begin{mylst}%
            \caption[\lst@@caption]{\hangindent0pt\lst@caption\vspace*{-\paperheight}}%
            \hrule width\textwidth height0pt%some how I need something width enough here
        \end{mylst}\vskip-2\baselineskip}%
    \makeatother
    \hrule
    \begin{lstlisting}[caption={[short caption for .lom]Although I cancel the hanindent this caption is still too long so please make sure your lstlisting code (or some remark you write after it or whatever) is long enough so that the next marginpar will not be overlapped.}]
        \This#is$the&new^lstlisting_.
    \end{lstlisting}
    \hrule

    What next?
\end{document}

更新

如果caption已加载,仍然有两条路可走:

  • 主要问题是 caption-box 被\parbox[b]{#1}{#2}毫无理由地定义为 ,因此将其重新定义为\parbox[t]{#1}{#2}
  • 或者,我们可以不将标题框的高度设为零,而是将其做得mylst非常高,然后附加一个适当的负跳过。

  • 顺便说一下,有一些关于@破坏 Mylst 列表的问题。因此,下面的代码\let\lstatatcaption\lst@@caption是必要的。
  • 间距仍然很混乱。
  • 毕竟,如果标题框太长,它就会超出底部边缘。如果您需要更复杂的内容,请发表评论并告诉我您期望什么样的行为。

以下是代码

\documentclass[a4paper,10pt]{scrreprt}
\usepackage{listings}
\usepackage[top=1cm,bottom=1cm,left=2cm,right=5cm]{geometry}
\usepackage{caption}
    \captionsetup{format=plain,font={small,it},labelfont={bf},belowskip=-10cm}
\usepackage{floatrow}
    \DeclareNewFloatType{mylst}{placement=H,name=Mylst,fileext=lom}
    \floatsetup{margins=hangright,floatwidth=\textwidth,capposition=beside,capbesideposition={top,right}}

\begin{document}
    \listof{mylst}{List of Mylst}
    \hrule
    \begin{figure}[h!]
        \caption{usual figure caption}
        so sad you cannot see the figure.
    \end{figure}
    \hrule
    \begin{lstlisting}
        \This#is$the&usual^lstlisting_.
    \end{lstlisting}
    \hrule
\begingroup
    \makeatletter
    \def\caption@parbox#1#2{\parbox[t]{#1}{#2}}
    \def\lst@makecaption#1#2{
        \begin{mylst}
            \let\lstatatcaption\lst@@caption
            \caption[\lstatatcaption]{\lst@caption\vspace*{-\paperheight}}
            \hrule width\textwidth height0pt
        \end{mylst}}
    \hrule
    \begin{lstlisting}[caption={[short caption for .lom]Although I cancel the hangindent this caption is still too long so please make sure your lstlisting code (or some remark you write after it or whatever) is long enough so that the next marginpar will not be overlapped.}]
        \This#is$even&newer^lstlisting_.
    \end{lstlisting}
\endgroup
    \hrule
    \vspace*{18cm}
    \hrule
\begingroup
    \makeatletter
    \def\lst@makecaption#1#2{
        \begin{mylst}
            \let\lstatatcaption\lst@@caption
            \caption[\lstatatcaption]{\lst@caption}
            \vrule width0ptheight\paperheight
        \end{mylst}
        \vspace*{-\paperheight}}
    \hrule
    \begin{lstlisting}[caption={[another short caption for .lom]This time caption package cancels the hangindent and mylst, being very very tall, follows immediately a negative skip which makes everything fine as before.}]
        \This#is$the&new^lstlisting_.
    \end{lstlisting}
\endgroup
    \hrule
    What next?
\end{document}

相关内容