编辑:甚至不必费心阅读问题。直接跳到Heiko Oberdiek 的回答 :)
使用listings
软件包,可以排版一些绝对字体大小非常简单;定义样式时,您所要做的就是将字体大小声明(可能还有其他宏)传递给listings
'basicstyle
键。例如:
\lstdefinestyle{mystyle}{basicstyle=\ttfamily\color{blue}\scriptsize}
lstlisting
对于显示代码(环境)和独立文件( )来说,这没问题\lstinputlisting
。但是,你很少想要内联代码(\lstinline
)以该格式排版绝对字体大小。例如,以下情况可能不是您想要的:
\documentclass{article}
\usepackage{xcolor}
\usepackage{listings}
\lstset{basicstyle=\ttfamily\color{blue}\scriptsize}
\begin{document}
\Large
Here is a very important keyword: \lstinline|foo|.
\end{document}
哎呀……“foo”排版在 中\scriptsize
,而周围的文本排版在 中\Large
。虽然您想保留大部分样式(此处为打字机字体和蓝色),但您可能希望内联代码覆盖字体大小规范并排版在当前字体大小, 正确的?
现在,如果你只是尝试传递\fontsize{\f@size}{\f@baselineskip}
给basicstyle
,你会失望的:
\documentclass{article}
\usepackage{xcolor}
\usepackage{listings}
\lstset{basicstyle=\ttfamily\color{blue}\scriptsize}
\begin{document}
\Large
Here is a very important keyword: \lstinline|foo|.
\makeatletter
Here is a very important keyword:
\lstinline[basicstyle=\fontsize{\f@size}{\f@baselineskip}\selectfont]|foo|.
\makeatother
\end{document}
你可以抽出手帕:你刚刚覆盖了当前的基本样式,结果,你失去了打字机字体和颜色规范!当然,你可以写
\lstinline%
[basicstyle=\ttfamily\color{blue}\fontsize{\f@size}{\f@baselineskip}\selectfont]%
|foo|
但是这种方法假设您知道原始定义basicstyle
(可能深埋在包内部,对于凡人来说太深了)并且是您希望避免的代码重复的典型示例。
因此我想出了以下解决方法,效果很好:
\documentclass{article}
\usepackage{xcolor}
\usepackage{listings}
\makeatletter
\newcommand\applyCurrentFontsize
{%
% we first save the current fontsize, baseline-skip,
% and listings' basicstyle
\let\f@sizeS@ved\f@size%
\let\f@baselineskipS@ved\f@baselineskip%
\let\basicstyleS@ved\lst@basicstyle%
% we now change the fontsize of listings' basicstyle
\renewcommand\lst@basicstyle%
{%
\basicstyleS@ved%
\fontsize{\f@sizeS@ved}{\f@baselineskipS@ved}%
\selectfont%
}%
}
\makeatother
\newcommand\scaledlstinline[2][]
{%
\bgroup%
\lstset{#1}%
\applyCurrentFontsize%
% ... possibly other macros whose effects should remain local ...
\lstinline|#2|%
\egroup%
}
\lstset{basicstyle=\ttfamily\color{blue}\scriptsize}
\begin{document}
\Large
Here is a very important keyword: \lstinline[]|foo|.
Here is a very important keyword: \scaledlstinline{foo}.
\end{document}
到目前为止,一切都很好……但是,当我尝试将此策略应用于 时,我遇到了麻烦\lstMakeShortInline
,listings
这是一个允许您为内联代码定义一个字符分隔符的宏(我已从listings.dtx
下面发布了相关行)。更具体地说,由于 的\lstMakeShortInline@
使用方式很奇怪\lst@shortinlinedef
,我不知道应该将分组命令放在哪里。
任何指导将不胜感激。
\newcommand\lstMakeShortInline[1][]{%
\def\lst@shortinlinedef{\lstinline[#1]}%
\lstMakeShortInline@}%
\def\lstMakeShortInline@#1{%
\expandafter\ifx\csname lst@ShortInlineOldCatcode\string#1\endcsname\relax
\lst@shortlstinlineinfo{Made }{#1}%
\lst@add@special{#1}%
% \end{macrocode}
% The character's current catcode is stored in
% |\lst@ShortInlineOldCatcode\|\meta{c}.
% \begin{macrocode}
\expandafter
\xdef\csname lst@ShortInlineOldCatcode\string#1\endcsname{\the\catcode`#1}%
% \end{macrocode}
% The character is spliced into the definition using the same trick as
% used in |\verb| (for instance), having activated |~| in a group.
% \begin{macrocode}
\begingroup
\catcode`\~\active \lccode`\~`#1%
\lowercase{%
% \end{macrocode}
% The character's old meaning is recorded
% in |\lst@ShortInlineOldMeaning\|\meta{c} prior to assigning it a new one.
% \begin{macrocode}
\global\expandafter\let
\csname lst@ShortInlineOldMeaning\string#1\endcsname~%
\expandafter\gdef\expandafter~\expandafter{\lst@shortinlinedef#1}}%
\endgroup
% \end{macrocode}
% Finally the character is made active.
% \begin{macrocode}
\global\catcode`#1\active
% \end{macrocode}
% If we suspect that \meta{c} is already a short reference, we tell
% the user. Now he or she is responsible if anything goes wrong\,\dots
% (Change in \packagename{listings}: We give a proper error here.)
% \begin{macrocode}
\else
\PackageError{Listings}%
{\string\lstMakeShorterInline\ definitions cannot be nested}%
{Use \string\lstDeleteShortInline first.}%
{}%
\fi}
答案1
包中listings
有两个钩子TextStyle
和DisplayStyle
/或一个开关\lst@ifdisplaystyle
,可以用来在内联和显示的代码列表中设置不同的字体大小,例如:
\documentclass{article}
\usepackage{color}
\usepackage{listings}
\makeatletter
\lstdefinestyle{mystyle}{
basicstyle=%
\ttfamily
\color{blue}%
\lst@ifdisplaystyle\scriptsize\fi
}
\makeatother
\lstset{style=mystyle}
\begin{document}
\Large
\noindent
Here is a very important keyword: \lstinline|foo|.
\begin{lstlisting}
Some code with foo.
\end{lstlisting}
\end{document}
答案2
@HeikoOberdiek 的解释他的回答以上低估了它的威力,事实上,仅仅基于他的解释,你不会认为他的答案完全解决了 OP 的问题(正如一位评论者反对的那样)。
Heiko 只是声称他提供了一种方法,用于“在内联和显示的代码列表中设置不同的字体大小”。但 OP 想要的是\lstinline
继承周围文本的字体大小。Heiko 的解决方案确实做到了这一点,尽管读者可能不太清楚。
因此,我只想举一个例子来说明,使用 Heiko 的解决方案,\lstinline
字体大小确实会增大和缩小以适应周围的文本。(如果 Heiko 想在他的答案中添加一个类似的例子,我很乐意删除这个。)
我\texttt{foo}
在每个后面直接添加一个\lstinline|foo|
,以便于比较两个的高度foo
。
\documentclass{article}
\usepackage{color}
\usepackage{listings}
\makeatletter
\lstdefinestyle{mystyle}{
basicstyle=%
\ttfamily
\color{blue}%
\lst@ifdisplaystyle\footnotesize\fi
}
\makeatother
\lstset{style=mystyle}
\begin{document}
\title{Here is a title with \lstinline|foo| \texttt{foo}.}
\date{}
\maketitle
\section{Here is a section heading with \lstinline|foo| \texttt{foo}.}
\Large
Here is: \lstinline|foo| \texttt{foo}.\\
\Huge
Here is: \lstinline|foo| \texttt{foo}.\\
\begin{lstlisting}
Some code with foo.
\end{lstlisting}
\end{document}