如何將程式碼清單放入 tikzposter \block 中?

如何將程式碼清單放入 tikzposter \block 中?

我試圖在海報中添加代碼列表,但出現以下錯誤:

TeX 容量超出,抱歉 [輸入堆疊大小=5000]

我究竟做錯了什麼?

\documentclass[portrait, a0paper, 25pt]{tikzposter}
\title{title}
\author{}
\institute{}
\usepackage{color}
\definecolor{bluekeywords}{rgb}{0.13,0.13,1}
\definecolor{greencomments}{rgb}{0,0.5,0}
\definecolor{redstrings}{rgb}{0.9,0,0}
\usepackage{listings}
\lstset{language=[Sharp]C,
showspaces=false,
showtabs=false,
breaklines=true,
showstringspaces=false,
breakatwhitespace=true,
escapeinside={(*@}{@*)},
commentstyle=\color{greencomments},
keywordstyle=\color{bluekeywords}\bfseries,
stringstyle=\color{redstrings},
basicstyle=\ttfamily
}
\begin{document}
\maketitle
\block{Block title}{
\begin{lstlisting}
%some code
\end{lstlisting}
}
\end{document}
\endinput

答案1

不...程式碼格式-...格式化...什麼...所以...永遠...我會...死...

更嚴重的是,問題是環境內容物lstlisting被處理逐字,這意味著命令的參數中不能有這樣的環境(\block此處)。

常見的解決方法是將清單寫入外部檔案(有或沒有套件filecontents)並使用命令插入\lstinputlisting,而不是將清單嵌入到lstlisting環境中。

在此輸入影像描述

\documentclass[portrait, a0paper, 25pt]{tikzposter}

\title{title}
\author{}
\institute{}

\usepackage{color}
\definecolor{bluekeywords} {rgb}{0.13, 0.13, 1}
\definecolor{greencomments}{rgb}{0   , 0.5 , 0}
\definecolor{redstrings}   {rgb}{0.9 , 0,    0}

\usepackage{listings}
\lstset{
  language          = [Sharp]C,
  showspaces        = false,
  showtabs          = false,
  breaklines        = true,
  showstringspaces  = false,
  breakatwhitespace = true,
  escapeinside      = {(*@}{@*)},
  commentstyle      = \color{greencomments},
  keywordstyle      = \color{bluekeywords}\bfseries,
  stringstyle       = \color{redstrings},
  basicstyle        = \ttfamily,
}

% -----
% the following writes to an external file called 'foo.hs'
\usepackage{filecontents}
\begin{filecontents*}{foo.hs}
fibs = 0 : 1 : zipWith (+) fibs (tail fibs)
\end{filecontents*}
% -----

\begin{document}
\maketitle
\block{Block title}{%
  \lstinputlisting{foo.hs}
}
\end{document}

相關內容