How to render code blocks with the same style as figures in PDF #6356
-
DescriptionI'm writing a book and just started learning quarto. When I render the document in PDF, figures and tables use the standard style for captions: one line captions are centered, multi-line captions are justified with "Figure X"/"Table X" at left. This is all ok for me. I'm using document class My text also include Python code. For example:
These listings are rendered in a very distinct way than figures and tables. The captions are always left aligned, the "Listing" text is bold face, there are lines above and below the caption text and a line at the bottom of the code too. Is there a setting to get listings rendered (in special the caption) the same way as figures and tables? Can I edit CSS styles somewhere? |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments
-
PDF book format uses LaTeX. CSS is for HTML. What you can do is use LaTeX and define your own environment. \begin{codelisting}
\caption{My simple code}
\hypertarget{lst-my-code}{%
\label{lst-my-code}}%
\begin{Shaded}
\begin{Highlighting}[]
\KeywordTok{def}\NormalTok{ f(n):}
\ControlFlowTok{return}\NormalTok{ n}\OperatorTok{*}\NormalTok{n}
\end{Highlighting}
\end{Shaded}
\end{codelisting} Where \@ifpackageloaded{float}{}{\usepackage{float}}
\floatstyle{ruled}
\@ifundefined{c@chapter}{\newfloat{codelisting}{h}{lop}}{\newfloat{codelisting}{h}{lop}[chapter]}
\floatname{codelisting}{Listing} Work ongoing (watch #4944) will add more flexibility in the cross-reference system which would allow you to do what you want. |
Beta Was this translation helpful? Give feedback.
-
The option I just solved this by adding:
I took me some time to figure out the #4944 is also great news! |
Beta Was this translation helpful? Give feedback.
The option
keep-tex: true
is a great one! I was using two formats:pdf
andlatex
. Now it's half the effort!I just solved this by adding:
I took me some time to figure out the
\floatstyle{ruled}
in the TeX file.#4944 is also great news!