
À primeira vista, isso pode parecer uma duplicata deoutra perguntajá fechado como uma duplicata deEste. Mas não é ;-)
Como faço para definirglobalmenteo tamanho da fonte de todas \lstinline
as macros, independente do tamanho da fonte dos lstlisting
ambientes?
O padrão que tenho para os ambientes é \small
, o que parece muito bom, mas preciso de tamanho normal para inlines, pois ele se mistura melhor com o texto ao redor.
Abaixo, conforme solicitado, um MWE mostrando a repetição de \lstset
macros:
\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}
Responder1
Que tal criar um novo comando como \myinline
no meu 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}
Responder2
Olhando no código, não existe esse método de distinguir entre os dois. Mas parece que conseguimos fazer um, porque \lstinline
ele roda \lstset
internamente, então poderíamos adicionar um patch. Não faço ideia se isso tem outras consequências.
\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}
Responder3
Salve \lstinline
com outro nome e redefina-o para passar o original também a opção basicstyle=\ttfamily
(sem \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}