更新

更新

我正在尝试创建一个包含两列的表格,其中第二列代表大量正则表达式,其中包含您能想到的所有字符。我尝试使用 \verb|text| 命令,但当有“%”符号时它会卡住,如果我尝试使用反斜杠转义它们,虽然可以工作,但结果比较为“...\%...”。有什么解决方案可以帮助我转义所有字符吗?

这是我的代码片段

\begin{table}[H]
\centering
\begin{tabularx}{\textwidth}{cX}
    \toprule
    $Placeholder$&$Regular~Expression$\\ 
    \midrule
    URL&(https?:\/\/)?(www\.)?[-a-zA-Z0-9@:%._\+~#=]{2,256}\.[a-z]{2,4}\b([-a-zA-Z0-9@:%_\+.~#?&//=]*)\\
    \bottomrule
\end{tabularx}
\caption{Regular Expressions}
\label{regex}\end{table}

更新

感谢您提供的解决方法,效果如预期!我之所以使用 tabularx 而不是 tabular 环境,是因为我需要手动将行拆分为正则表达式单元格;这个解决方案似乎仍然不可行,但总比没有好 :)

答案1

正如@DavidCarlisle 在评论中指出的那样,您可以按如下方式进行:使用环境tabular而不是tabularx环境,使用p第二列的列类型,并使用有关第一列宽度的信息计算其宽度。此设置允许使用宏\url来排版长正则表达式字符串,就像它是 URL 字符串一样。

在下面的例子中,使用{and/or}是允许的,因为即使这些字符出现在正则表达式字符串中,它们也会以正确的顺序出现并且是平衡的。如果不是这种情况,即如果花括号不平衡,则必须使用在正则表达式字符串中任何地方都没有出现的字符;例如,\url!...!\url M...M都可以工作,因为!nor 都不M出现在正则表达式字符串中。

在此处输入图片描述

\documentclass{article}
\usepackage{booktabs}
\usepackage[spaces,hyphens]{url}

\newlength\lengtha
\newlength\lengthb
% Choose longest string in column A to calculate width
\settowidth{\lengtha}{\emph{Placeholder}}     
% Calculate width of column B as a residual
\setlength\lengthb{\dimexpr\textwidth-2\tabcolsep-\lengtha\relax} 

\begin{document}
\begin{table}
\begin{tabular}{@{} l p{\lengthb} @{}}
    \toprule
    \emph{Placeholder}&\emph{Regular~Expression}\\
    \midrule
    URL&  \url{(https?:\/\/)?(www\.)?[-a-zA-Z0-9@:%._\+~#=]{2,256}\.[a-z]{2,4}\b([-a-zA-Z0-9@:%_\+.~#?&//=]*)} \\
    \bottomrule
\end{tabular}
\caption{Regular Expressions}
\label{regex}\end{table}
\end{document}

答案2

由于tabularx收集了其内容,直接使用是\verb行不通的。此外,还\verb需要一个不在文本中的字符来捕获。\detokenize需要平衡的文本。

向世界宣布一个逐字捕获宏

这个想法是捕获任意字符(但是假设标准 catcode 自然,否则可以设置一个循环来设置所有 catcode)并将其放入宏中(用 无法实现\verb)。语法是:

\literalset\foo<SPACE>ARBITRARY CHARACTERS<END OF LINE>

请注意,输入中的空格在<END OF LINE> 请注意,在will不被捕获在宏中\foo<SPACE>开头的 是必需的,并在处理过程中被删除。文字输入末尾以外的空格将被捕获。

代码:

\documentclass[a4paper]{article}
\usepackage[T1]{fontenc}
\usepackage[margin=.5cm]{geometry}
\usepackage{tabularx}

\makeatletter
% \literalset\foo<SPACE>ARBITRARY CHARACTERS<END OF LINE>
\def\literalset #1{% assumes standard \endlinechar
    \begingroup
    \def\x{#1}%
    \catcode`\^^M 2
    \let\do\@makeother
    \dospecials
    \afterassignment\literalset@i
    \toks0=\bgroup }%
\def\literalset@i 
   {\expandafter\xdef\x{\expandafter\@gobble\the\toks0}\endgroup}
\makeatother

\begin{document}


% I have added a space between \/)? and (www compared to original.

\literalset\foo (https?:\/\/)? (www\.)?[-a-zA-Z0-9@:%._\+~#=]{2,256}\.[a-z]{2,4}\b([-a-zA-Z0-9@:%_\+.~#?&//=]*)\\
Hello, all is fine here ? I hope so.

\typeout{I AM HERE: \meaning\foo}

\meaning\foo

\begin{table}[htbp]
\centering
\begin{tabularx}{\textwidth}{cX}
    \hline
    Placeholder&Regular Expression\\ 
    \hline
    URL&\texttt{\foo}\\
    \hline
\end{tabularx}
\caption{Regular Expressions}
\label{regex}
\end{table}

\end{document}

请注意,这种去标记字符的非常长的序列的换行是另一个问题,现在可以添加一个\printliteral命令,在每个字符后添加断点。

引用


更新以添加承诺的\printliteral命令。请参阅代码注释以了解解释和上下文。

\documentclass[a4paper]{article}
\usepackage[T1]{fontenc}
%\usepackage[margin=.5cm]{geometry}
\usepackage{tabularx}

\makeatletter
% \literalset\foo<SPACE>ARBITRARY CHARACTERS<END OF LINE>
\def\literalset #1{% assumes standard \endlinechar
    \begingroup
    \def\x{#1}%
    \catcode`\^^M 2
    \let\do\@makeother
    \dospecials
    \afterassignment\literalset@i
    \toks0=\bgroup }%
\def\literalset@i 
   {\expandafter\xdef\x{\expandafter\@gobble\the\toks0}\endgroup}
\makeatother

% TeX has no toggle to tell it to break long words (of random
% characters) automatically when reaching end of line: it goes
% to the right margin and beyond in absence of hyphens and
% spaces if confronted to a non-interrupted sequence of
% characters. And in a \texttt, breaking at hyphens is usually
% inihibited.

% Here is a very simple-minded macro which allows to print a
% \foo which has been declared by \literalset, with automatic
% breaks. More sophisticated treatment is possible (e.g. use
% of discretionaries to allow insertion of continuation
% symbols at breaks).

% We add a little stretch to avoid underfull/overfull boxes.

\makeatletter
\def\printliteral   #1{\expandafter\printliteral@i#1\relax }%
\def\printliteral@i #1{\if\relax #1\else\hskip\z@ \@plus .4\p@\relax
                       #1\expandafter\printliteral@i \fi}
\makeatother

\begin{document}


% I have added a space between \/)? and (www compared to original.

\literalset\foo (https?:\/\/)? (www\.)?[-a-zA-Z0-9@:%._\+~#=]{2,256}\.[a-z]{2,4}\b([-a-zA-Z0-9@:%_\+.~#?&//=]*)\\
Hello, all is fine here ? I hope so.

\typeout{I AM HERE: \meaning\foo}

\printliteral{\meaning\foo}

\begin{table}[htbp]
\centering
%\begin{tabularx}{\textwidth}{c>{\raggedright\arraybackslash}X}
\begin{tabularx}{\textwidth}{cX}
    \hline
    Placeholder&Regular Expression\\ 
    \hline
    URL&\texttt{\printliteral\foo}\\
    \hline
\end{tabularx}
\caption{Regular Expressions}
\label{regex}
\end{table}
%\showoutput

\end{document}

引用

相关内容