0

Looking to display several lines of code in my report as one block, the same way that code is displayed on Stack Exchange. For inline code I've been using \tikzhl and \texttt:

\usepackage{tikz}
\newcommand*{\tikzhl}[1]{\tikz[baseline=(X.base)]
 \node[fill=black!10] (X) {#1};}

This seems a good solution for code integrated in paragraphs: enter image description here

But for several lines as their own paragraph it is not ideal: enter image description here What I would like to do is have it appear as the following with one bounding grey box:

Found 20 valid images for calibration
DIM=(1920, 1080)
K=np.array([[1374.9140598345757, 0.0, 996.2559942548266], [0.0, 1376.7481511557473, 541.0310811091442], [0.0, 0.0, 1.0]])
D=np.array([[-0.017077674858759682], [-0.30267173652198426], [1.7521950786149205], [-3.054359786737165]])

Adding a new line where it is wider than the page margins if possible, although I can do this manually if not possible.

Thanks in advance. Using Texmaker with MiKTeX 2.9.

2
  • 1
    You have listings and minted specifically for this purpose. TikZ should not be used in this case.
    – user156344
    Apr 7, 2019 at 15:56
  • 1
    If you want to customize more that what these two packages can handle, you can use tcolorbox with listings library.
    – user156344
    Apr 7, 2019 at 15:57

1 Answer 1

2

I keep your \tikzhl command, but it won't work with something like \tikzhl{\documentclass} (which is supposed to output \documentclass).

\documentclass{article}
\usepackage{listings}
\usepackage{xcolor}
\lstset{
    basicstyle=\ttfamily,
    backgroundcolor=\color{black!10},
    breaklines=true
}
\usepackage{tikz}
\newcommand*{\tikzhl}[1]{\tikz[baseline=(X.base)]
 \node[fill=black!10,font=\ttfamily,inner sep=1pt] (X) {#1};}
\begin{document}
\begin{lstlisting}
Found 20 valid images for calibration
DIM=(1920, 1080)
K=np.array([[1374.9140598345757, 0.0, 996.2559942548266], [0.0, 1376.7481511557473, 541.0310811091442], [0.0, 0.0, 1.0]])
D=np.array([[-0.017077674858759682], [-0.30267173652198426], [1.7521950786149205], [-3.054359786737165]])
\end{lstlisting}

Package \tikzhl{listings} can do it for you.
\end{document}

enter image description here

0

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .