如何调整 fancyvrb verbatiminput 文本以适合 tabularx 单元格

如何调整 fancyvrb verbatiminput 文本以适合 tabularx 单元格

我想将文件中的文本包含到 tabularx 单元格中,但遇到两个问题:

  • 逐字输入文本溢出定义边距
  • 单元格有额外的垂直边距

我该如何修复这个问题?

捕获

代码

\documentclass[a4paper,11pt]{article}
\usepackage[a4paper,
inner= 1.5cm,
outer= 3cm,
top = 2cm,
bottom = 3cm,
bindingoffset = 0.5cm]{geometry}
\usepackage[T1]{fontenc}
\usepackage[utf8]{inputenc}
\usepackage{lmodern}
\usepackage[spanish]{babel}
\usepackage{hyphenat}
\usepackage{tabularx}
\usepackage{booktabs}
\usepackage[table,xcdraw]{xcolor}
\usepackage{verbatim}
\usepackage{fancyvrb}
\usepackage{blindtext}

\setlength{\parindent}{0pt}
\setlength{\parskip}{2pt}
\setlength{\extrarowheight}{1mm}

\renewcommand*\familydefault{\sfdefault} % Set Arial as default

\RecustomVerbatimCommand{\VerbatimInput}{VerbatimInput}
{
 fontfamily = courier,
 fontsize = \footnotesize,
 frame = single,
 vspace = 0pt,
 framesep = 0pt,
 framerule = 0.5pt,
 rulecolor=\color{red},
 baselinestretch = 0.2
}
    
\begin{document}

    \begin{tabularx}{\textwidth}{X X}
        \multicolumn{2}{l}{\cellcolor[HTML]{8DB3E2}A table} \\
        \toprule
            cmd arg $--$switch1 $--$switch2 $--$switch3 $--$switch4 $--$paramY  999 &
            \blindtext \\
        \midrule
            cmd2 &
            \VerbatimInput{files/f1.txt}
            \\
        \bottomrule
    \end{tabularx} 
    \vspace{1pt}
    \newline    

\end{document}

f1 文件

Paper size: 4L
Ink capacicy: 0.5oz 

The random-option changes the default blind text to a sequence of predefined
sentences. The next paragraph starts with the next phrase from the previous
paragraph.
# $ % & ~ _ ^ \ { }

答案1

你可以使用包裹listings输入带有自动换行符的 varbatim 文本。在下面的例子中,我将换行符限制为空格:

\begin{filecontents*}{\jobname.txt}
Paper size: 4L
Ink capacicy: 0.5oz 

The random-option changes the default blind text to a sequence of predefined
sentences. The next paragraph starts with the next phrase from the previous
paragraph.
# $ % & ~ _ ^ \ { }
\end{filecontents*}
\documentclass[a4paper,11pt]{article}
\usepackage[a4paper,
inner= 1.5cm,
outer= 3cm,
top = 2cm,
bottom = 3cm,
bindingoffset = 0.5cm]{geometry}
\usepackage[T1]{fontenc}
\usepackage[utf8]{inputenc}
\usepackage{lmodern}
\usepackage[spanish]{babel}
\usepackage{hyphenat}
\usepackage{tabularx}
\usepackage{booktabs}
\usepackage[table,xcdraw]{xcolor}
\usepackage{listings}
\usepackage{blindtext}

\setlength{\parindent}{0pt}
\setlength{\parskip}{2pt}
\setlength{\extrarowheight}{1mm}

\renewcommand*\familydefault{\sfdefault} % Set Arial as default

\lstset{basicstyle=\ttfamily\footnotesize,breaklines=true,breakatwhitespace=true}
    
\begin{document}

    \begin{tabularx}{\textwidth}{X X}
        \multicolumn{2}{l}{\cellcolor[HTML]{8DB3E2}A table} \\
        \toprule
            cmd arg $--$switch1 $--$switch2 $--$switch3 $--$switch4 $--$paramY  999 &
            \blindtext \\
        \midrule
            cmd2 &
            \lstinputlisting[frame=single,rulecolor=\color{red},aboveskip=-\baselineskip,belowskip=-\baselineskip]{\jobname.txt}
            \\
        \bottomrule
    \end{tabularx} 
    \vspace{1pt}
    \newline    

\end{document}

使用列表

还有许多其他选项,例如,显示自动换行符。

相关内容