列表包并设置 lstinline 样式?

列表包并设置 lstinline 样式?

我在讲义中为 3 种不同的代码风格环境(\lstdefinestyle\lstnewenvironment)设置了 3 种风格。我该如何将\lstinline风格设置为其中之一?或者这不可能吗?

因为lstinline现在的字体非常小,并且与正常文本不一致。(文档真的没有什么帮助?:/)

以下是示例代码

\documentclass[12pt,twoside,a4paper,fleqn,ngerman]{report} %Koma-Script Class article

\RequirePackage{listings}               % Programmierprachen
\let\l\lstinline
\let\t\text
\RequirePackage{lmodern}                % customized font size 
\RequirePackage{color}
\definecolor{mygray}{rgb}{0.31,0.31,0.31}

% style for appendix with smaller size for the appendix with custom size 9pt
\lstdefinestyle{small_customf95}{
    % belowcaptionskip=1\baselineskip,
    breaklines=true,
    frame=none,
    xleftmargin=0.5cm,
    language=[95]Fortran,
    showstringspaces=false,
    basicstyle=\footnotesize\ttfamily,
    keywordstyle=\bfseries,
    commentstyle=\color{mygray},
    % identifierstyle=\color{blue},
    % stringstyle=\color{orange},
}
\lstset{mathescape=true,escapeinside={@}{@},style=small_customf95}

\lstnewenvironment{smallf95}{%
    \lstset{%
        mathescape=true,escapeinside={@}{@},style=small_customf95,
    }
}{}

% style for appendix with smaller size for the appendix with custom size 9pt, very small !!! only use
%if the other size is still to big !!!
\lstdefinestyle{smaller_customf95}{
    % belowcaptionskip=1\baselineskip,
    breaklines=true,
    frame=none,
    xleftmargin=0.5cm,
    language=[95]Fortran,
    showstringspaces=false,
    basicstyle=\fontsize{9pt}{9pt}\ttfamily,
    keywordstyle=\bfseries,
    commentstyle=\color{mygray},
    % identifierstyle=\color{blue},
    % stringstyle=\color{orange},
}
\lstset{mathescape=true,escapeinside={@}{@},style=smaller_customf95}

\lstnewenvironment{smallerf95}{%
    \lstset{%
        mathescape=true,escapeinside={@}{@},style=smaller_customf95,
    }
}{}

%default style for F95 code for the actual lectures, size 8pt
\lstdefinestyle{customf95}{
    % belowcaptionskip=1\baselineskip,
    breaklines=true,
    frame=none,
    xleftmargin=0.5cm,
    language=[95]Fortran,
    showstringspaces=false,
    basicstyle=\small\ttfamily,
    keywordstyle=\bfseries,
    commentstyle=\color{mygray},
    % identifierstyle=\color{blue},
    % stringstyle=\color{orange},
}
%\lstset{mathescape=true,escapeinside={@}{@},style=customf95}
\lstnewenvironment{basicf95}{%
    \lstset{%
        mathescape=true,escapeinside={@}{@},style=customf95
    }
}{}

\begin{document}
    test \l{ADVANCE=?}.
\begin{basicf95}
    PROGRAM test
        IF blabla
            ...
        END IF
    END PROGRAM test
\end{basicf95}
\l{ADVANCE=?} - now?
\end{document}

答案1

只需定义风格并在定义环境时做出适当的调整:整体风格的变化也将反映在所有三个环境和内联版本中。

\documentclass[12pt,twoside,a4paper,fleqn,ngerman]{report} %Koma-Script Class article
\usepackage[T1]{fontenc}
\usepackage{babel}

\usepackage{listings}               % Programmierprachen
\usepackage{lmodern}                % customized font size 
\usepackage{color}

\definecolor{mygray}{rgb}{0.31,0.31,0.31}

% style for appendix with smaller size for the appendix with custom size 9pt

\lstdefinestyle{customf95}{
    % belowcaptionskip=1\baselineskip,
    breaklines=true,
    frame=none,
    xleftmargin=0.5cm,
    language=[95]Fortran,
    showstringspaces=false,
    keywordstyle=\bfseries,
    commentstyle=\color{mygray},
    % identifierstyle=\color{blue},
    % stringstyle=\color{orange},
    mathescape=true,
    escapeinside={@}{@},
}

\lstnewenvironment{basicf95}
 {%
  \lstset{
    style=customf95,
    basicstyle=\small\ttfamily,
  }%
 }{}

\lstnewenvironment{smallf95}
 {%
  \lstset{
    style=customf95,
    basicstyle=\footnotesize\ttfamily,
  }%
 }{}
\lstnewenvironment{smallerf95}
 {%
  \lstset{
    style=customf95,
    basicstyle=\fontsize{9pt}{11pt}\ttfamily,
  }%
 }{}
\newcommand{\inlinef}[1][]{%
  \lstinline[style=customf95,basicstyle=\ttfamily,#1]%
}


\begin{document}

test \inlinef{ADVANCE=?}.

\begin{basicf95}
    PROGRAM test
        IF blabla
            ...
        END IF
    END PROGRAM test
\end{basicf95}

\inlinef{ADVANCE=?} - now?

\begin{smallerf95}
    PROGRAM test
        IF blabla
            ...
        END IF
    END PROGRAM test
\end{smallerf95}

\inlinef{ADVANCE=?} - now?

\begin{smallf95}
    PROGRAM test
        IF blabla
            ...
        END IF
    END PROGRAM test
\end{smallf95}

\inlinef{ADVANCE=?} - now?

\end{document}

避免重新定义\l\t当您的参考书目包含某些波兰作家的名字时,您会后悔重新定义\l

在此处输入图片描述

答案2

手册说,这\lstinline需要一个可选参数,您可以在其中设置样式:

test \lstinline[style=customf95]{ADVANCE=?}.

在此处输入图片描述

另外,如果您需要简短的内联代码,您可以定义

\lstMakeShortInline[style=customf95]|

可以像短视频

test |ADVANCE=?|.

相关内容