
編輯: 甚至不用費心閱讀問題。直接跳到海科·奧伯迪克的回答 :)
與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
s 的高度。
\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}