
편집하다: 질문을 읽는 데 신경 쓰지 마세요. 그냥 바로 뛰어가세요하이코 오버디크의 답변 :)
패키지 를 사용하면 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}
\scriptsize
이런... "foo" 는 로 조판되는 주변 텍스트와 달리 로 조판됩니다 \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의 솔루션은 이를 수행하지만 독자에게는 그것이 명확하지 않습니다.
\lstinline
이러한 이유로 저는 실제로 Heiko의 솔루션을 사용하면 주변 텍스트에 맞게 글꼴 크기가 커지고 작아지는 것을 보여주는 예를 제공하고 싶습니다 . (Heiko가 그의 답변에 비슷한 예를 추가하고 싶다면 이 예를 기꺼이 삭제하겠습니다.)
두 s의 높이를 쉽게 비교할 수 있도록 \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}