インラインコードを現在のフォントサイズに合わせて拡大縮小する

インラインコードを現在のフォントサイズに合わせて拡大縮小する

編集: 質問を読む必要はありません。すぐにハイコ・オーバーディークの回答 :)

パッケージでは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}

ここに画像の説明を入力してください

ここまでは順調です...しかし、この戦術を に適用しようとすると、問題が発生します。 は\lstMakeShortInlinelistingsインライン コードの 1 文字の区切り文字を定義できるマクロです (関連する行を以下に掲載しています)。具体的には、 が を特殊な方法で使用している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には 2 つのフックTextStyleDisplayStyleスイッチがあり\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 が回答に同様の例を追加したい場合は、喜んでこの例を削除します。)

2 つの の高さを比較しやすくするために、\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}

関連情報