如何**仅**更改 \lstinline{} 的字体大小?

如何**仅**更改 \lstinline{} 的字体大小?

乍一看,这可能看起来像是另一个问题已经关闭,因为重复了这个. 但事实并非如此 ;-)

我如何设置全球所有宏的字体大小\lstinline,与环境的字体大小无关lstlisting

我所设置的默认环境是\small,它看起来非常好,但我需要内联的正常大小,因为它可以更好地与周围的文本混合。

下面,根据要求,MWE 显示\lstset宏的重复:

\documentclass{article}
\usepackage{listings}
\lstset{basicstyle=\ttfamily\small,numbers=left}

\begin{document}

\begin{lstlisting}
Displayed code listings
    Look great
In a small font
\end{lstlisting}

I would like \lstinline{inline listings} to be at normalsize as in
\lstset{basicstyle=\ttfamily}%
\lstinline{this piece of code}
but then I have to issue a new \verb|lstset| macro
\lstset{basicstyle=\ttfamily\small}%
in order to get back to small size

\begin{lstlisting}
for displayed code listings
    to show appropiately.
again
\end{lstlisting}

\end{document}

在此处输入图片描述

答案1

那么如何像\myinline我的 MWE 中那样创建一个新命令呢:

\documentclass{article}
\usepackage{listings}
\lstset{basicstyle=\ttfamily\small,numbers=left}
\newcommand{\myinline}[1]{\lstinline[basicstyle={\ttfamily\normalsize}]{#1}}

\begin{document}

\begin{lstlisting}
Displayed code listings
    Look great
In a small font
\end{lstlisting}

I would like \lstinline{inline listings} to be at normalsize as in
\myinline{this piece of code}
but then I have to issue a new \verb|lstset| macro
in order to get back to small size

\begin{lstlisting}
for displayed code listings
    to show appropiately.
again
\end{lstlisting}

\end{document}

在此处输入图片描述

答案2

查看代码,没有区分两者的方法。但我们似乎可以创建一个,因为它\lstinline确实在内部运行\lstset,所以我们可以添加补丁。不知道这是否会产生其他后果。

\documentclass[a4paper]{article}
\usepackage{listings,xcolor,xpatch}

\lstset{
  basicstyle=\color{red}\ttfamily,
}

% style for inline
\lstdefinestyle{foobar}{
  basicstyle=\color{green}\ttfamily\tiny,
}

% \ShowCommand\lstinline shows \lstinline runs \lstset{flexiblecolumns,#1}, so we mess with that
\makeatletter
\xpatchcmd\lstinline{flexiblecolumns,}{flexiblecolumns,style=foobar,}{}{\typeout{patch failed}}
\makeatother


\begin{document}

\begin{lstlisting}
FooBar
\end{lstlisting}

inline \lstinline|FooBar|
\end{document}

在此处输入图片描述

答案3

用另一个名称保存\lstinline并重新定义它以将原始名称也传递该选项basicstyle=\ttfamily(不带\small)。

\documentclass{article}
\usepackage{listings}

\lstset{basicstyle=\ttfamily\small,numbers=left}

\NewCommandCopy{\originallstinline}{\lstinline}
\RenewDocumentCommand{\lstinline}{O{}}{%
  \originallstinline[basicstyle=\ttfamily,#1]%
}

\begin{document}

\begin{lstlisting}
Displayed code listings
    Look great
In a small font
\end{lstlisting}

Here I see that \lstinline{inline listings} is at normalsize
and there is no need to issue a new \verb|lstset| macro
in order to get back to small size

\begin{lstlisting}
for displayed code listings
    to show appropriately.
again
\end{lstlisting}

We can also pass options like in \lstinline[language=TeX]|\LaTeX|

\end{document}

在此处输入图片描述

相关内容