我有两个并排的列表框,用于比较输入和输出代码。如何让 LaTeX 隐藏打印在列表框外的代码?
以下是 LaTeX 代码:
\documentclass[a4paper,export]{report}
\usepackage[utf8]{inputenc}
\usepackage[french]{babel}
\usepackage[T1]{fontenc}
\usepackage{lmodern}
\usepackage{xcolor}
\usepackage{graphicx}
\usepackage{listings}
\lstset{
%numbers=left,numberstyle=\tiny, stepnumber=1, numbersep=5pt,
breaklines=true,
breakatwhitespace=false,
tabsize=2,
basicstyle=\footnotesize,%\ttfamily,
frame=single,
rulecolor=\color{gray}
}
\usepackage{adjustbox}
\begin{document}
\noindent\begin{minipage}{.45\textwidth}
\begin{lstlisting}[caption=code 1,frame=tlrb,breaklines=false]{Name}
<?xml version="1.0" encoding="UTF-8"?>
<s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/">
<s:Body>
<GetSpecialtiesResponse xmlns="http://tempuri.org/">
<GetSpecialtiesResult xmlns:a="http://schemas.datacontract.org/2004/07/WsAgenda.DataContracts" xmlns:i="http://www.w3.org/2001/XMLSchema-instance">
<a:Specialty>
<a:code>cardiology</a:code>
<a:name>Cardiologie</a:name>
</a:Specialty>
<a:Specialty>
<a:code>neurology</a:code>
<a:name>Neurologie</a:name>
</a:Specialty>
<a:Specialty>
<a:code>urology</a:code>
<a:name>Urologie</a:name>
</a:Specialty>
<a:Specialty>
<a:code>physiotherapy</a:code>
<a:name>Physiotherapie</a:name>
</a:Specialty>
</GetSpecialtiesResult>
</GetSpecialtiesResponse>
</s:Body>
</s:Envelope>
\end{lstlisting}
\end{minipage}\hfill
\begin{minipage}{.45\textwidth}
\begin{lstlisting}[caption=code 2,frame=tlrb,breaklines=true]{Name}
{"Specialties": [
{
"id": "cardiology",
"description": "Cardiologie"
},
{
"id": "neurology",
"description": "Neurologie"
},
{
"id": "urology",
"description": "Urologie"
},
{
"id": "physiotherapy",
"description": "Physiotherapie"
}
]}
\end{lstlisting}
\end{minipage}
\end{document}
PDF 结果如下:
答案1
viewport
这是使用该软件包为其框命令提供的选项的版本adjustbox
。代码必须重新排列一点,以便列表周围的框架由 添加\adjustbox
,标题由 添加\captionof
。否则,您将得到缺失的框架右侧。
所有内容都包装到一个新的环境中codebox
,该环境将框宽度作为其第一个参数,将列表的标题作为其第二个参数。这基本上只是想法的草图;可以通过为环境提供键值选项列表来添加更好的界面以实现更多自定义。
完整示例代码:
\documentclass[a4paper,export]{report}
\usepackage{xcolor}
\usepackage{lipsum}
\usepackage{listings}
\usepackage{caption}
\usepackage{adjustbox}
\lstset{
%numbers=left,numberstyle=\tiny, stepnumber=1, numbersep=5pt,
breaklines=true,
breakatwhitespace=false,
tabsize=2,
basicstyle=\footnotesize,%\ttfamily,
% frame=single,
% rulecolor=\color{gray}
}
\newenvironment{codebox}[2]{%
\begin{minipage}[t]{#1}%
\vspace*{-0.5\baselineskip}
\captionof{lstlisting}{#2}\par
\vspace*{-0.7\baselineskip}
\begin{adjustbox}{
cfbox=gray,
clip=true,
viewport={\fboxrule+\fboxsep} {-\depth} {\linewidth-\fboxrule-\fboxsep} {\height}
}%
\hbadness=100
\hfuzz=\maxdimen
}{%
\end{adjustbox}%
\end{minipage}%
}
\begin{document}
\lipsum[1]
\noindent
\begin{codebox}{0.48\textwidth}{Code 1}
\begin{lstlisting}[breaklines=false]
<?xml version="1.0" encoding="UTF-8"?>
<s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/">
<s:Body>
<GetSpecialtiesResponse xmlns="http://tempuri.org/">
<GetSpecialtiesResult xmlns:a="http://schemas.datacontract.org/2004/07/WsAgenda.DataContracts" xmlns:i="http://www.w3.org/2001/XMLSchema-instance">
<a:Specialty>
<a:code>cardiology</a:code>
<a:name>Cardiologie</a:name>
</a:Specialty>
<a:Specialty>
<a:code>neurology</a:code>
<a:name>Neurologie</a:name>
</a:Specialty>
<a:Specialty>
<a:code>urology</a:code>
<a:name>Urologie</a:name>
</a:Specialty>
<a:Specialty>
<a:code>physiotherapy</a:code>
<a:name>Physiotherapie</a:name>
</a:Specialty>
</GetSpecialtiesResult>
</GetSpecialtiesResponse>
</s:Body>
</s:Envelope>
\end{lstlisting}
\end{codebox}
\hfill
\begin{codebox}{.48\textwidth}{Code 2}
\begin{lstlisting}[breaklines=true]
{"Specialties": [
{
"id": "cardiology",
"description": "Cardiologie"
},
{
"id": "neurology",
"description": "Neurologie"
},
{
"id": "urology",
"description": "Urologie"
},
{
"id": "physiotherapy",
"description": "Physiotherapie"
}
]}
\end{lstlisting}
\end{codebox}
\medskip
\lipsum[2]
\end{document}
输出
答案2
好吧,到目前为止,我发现实现我想要的唯一方法是删除 tex 源中的溢出代码。这不是一个优雅的解决方案,但它解决了这个问题:
\noindent\begin{minipage}[t]{.52\textwidth}
\begin{lstlisting}[caption=Réponse SOAP du web service agenda à la demande de la liste des spécialités,label={lst:getspecSOAP},frame=tlrb,breaklines=false]
<?xml version="1.0" encoding="UTF-8"
<s:Envelope xmlns:s="http://schemas.
<s:Body>
<GetSpecialtiesResponse xmlns="http:
<GetSpecialtiesResult xmlns:a="http:
<a:Specialty>
<a:code>cardiology</a:code>
<a:name>Cardiologie</a:name>
</a:Specialty>
<a:Specialty>
<a:code>neurology</a:code>
<a:name>Neurologie</a:name>
</a:Specialty>
<a:Specialty>
<a:code>urology</a:code>
<a:name>Urologie</a:name>
</a:Specialty>
<a:Specialty>
<a:code>physiotherapy</a:code>
<a:name>Physiotherapie</a:name>
</a:Specialty>
</GetSpecialtiesResult>
</GetSpecialtiesResponse>
</s:Body>
</s:Envelope>
\end{lstlisting}
\end{minipage}\hfill
\begin{minipage}[t]{.45\textwidth}
\begin{lstlisting}[caption=Réponse JSon produite par le datamapper,label={lst:getspecREST},frame=tlrb,breaklines=true]
{"Specialties": [
{
"id": "cardiology",
"description": "Cardiologie"
},
{
"id": "neurology",
"description": "Neurologie"
},
{
"id": "urology",
"description": "Urologie"
},
{
"id": "physiotherapy",
"description": "Physiotherapie"
}
]}
\end{lstlisting}
\end{minipage}
结果是: