lstlisting
我正在尝试将代码列表放在表格单元格中。除了将所有行合并为一行之外,它在大多数情况下都可以正常工作。MWE 显示了这个问题。有什么想法可以做到这一点吗?
使用的原因tabulary
,我需要表中的四件事:
- 打破常规的能力,
- 多列,
- 代码清单,
- 并自动调整线宽。
我尝试过使用tabular
,tabularx
但它们都无法与我使用 所能完成的功能相提并论tabulary
。 只有两个悬而未决的问题tabulary
:
- 列表不换行
- 我无法显示管道字符!(我不确定这个管道字符问题是否可以在这里得到解答,或者需要一个新的 stackexchage 帖子。如果需要,请见谅。)
我的 MWE:
\documentclass[a4paper,12pt]{book}
\usepackage[english]{babel}
\usepackage{blindtext}
\usepackage[most]{tcolorbox}
\usepackage{csquotes}
\usepackage{hyperref}
\usepackage{graphicx}
\usepackage{listings}
\usepackage{color}
\definecolor{dkgreen}{rgb}{0,0.6,0}
\definecolor{gray}{rgb}{0.5,0.5,0.5}
\definecolor{mauve}{rgb}{0.58,0,0.82}
\definecolor{bred}{rgb}{0.80,0,0}
\lstdefinestyle{Xcode} {
language = Java,
basicstyle = footnotesizettfamily,
showspaces = false,
showstringspaces = false,
breakautoindent = true,
flexiblecolumns = true,
keepspaces = true,
stepnumber = 0,
xleftmargin = 0pt}
\lstset{
style = Xcode,
breaklines=false,
frame=single,
basicstyle={\small\ttfamily},
numbers=none,
numberstyle=\tiny\color{gray},
keywordstyle=\color{blue},
commentstyle=\color{dkgreen},
stringstyle=\color{mauve},
breaklines=true,
breakatwhitespace=true,
tabsize=3,
escapeinside={(*}{*)},
breaklines=true
}
\let\oldtexttt\texttt% Store \texttt
\usepackage{tabulary}
\usepackage{ragged2e}
\renewcommand{\texttt}[2][black]{\sloppy \textcolor{#1}{\ttfamily #2}}% \texttt[<color>]{<stuff>}
\usepackage{datetime}
\begin{document}
Dummy text here.
\begin{center}
\begin{tabulary}{\textwidth}{|L|L|}
\hline
\multicolumn{2}{|l|}{\textbf{Dummy Heading 1}}\\ \hline
\multicolumn{2}{|p{\linewidth}|}{Dummy description 1. Test \textbf{bold} aaa aaaa aaa \texttt[blue]{tt blue} bbb bbbb}\\ \hline
Colum1 Header(s) & Description and Examples etc\\ \hline
\textbf{+, -, *, /, PIPECHAR}
\newline (Binary) \newline
Unable to display pipe character here
& \textbf{Addition, subtraction, multiplication,} and \textbf{division}.\newline \newline Example:\newline
I want to put code listing in this cell as follows. But all lines are being merged into a single line.
\begin{lstlisting}[language=java, basicstyle={\small\ttfamily}, moredelim={[is][\color{red}]{STARTRC}{ENDRC}}]^^J
int a = 10;
Integer b = 100; //using primitive wrapper here
int c = a + b;
\end{lstlisting}
I am using texttt on each line as altertive but this alternative cannot highlight language syntax
\texttt{int a = 10;}\newline
Integer b = 100; //using primitive wrapper here\newline
int c = a + b;\newline
other text here \\ \hline
\end{tabulary}
\end{center}
The same listing works fine outside the table :
\begin{lstlisting}[language=java, basicstyle={\small\ttfamily}, moredelim={[is][\color{red}]{STARTRC}{ENDRC}}]
int a = 10;
Integer b = 100; //using primitive wrapper here
int c = a + b;
\end{lstlisting}
\end{document}