
我正在嘗試創建一個包含兩列的表,其中第二列表示大量正則表達式以及您可以想像的所有字元。我嘗試使用 \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
都可以工作,因為!
和 都不會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
平衡的案文。
向世界宣布捕捉逐字宏
這個想法是捕獲任意字元(但是假設標準catcodes自然,否則可以設定一個循環來設定所有catcodes)並將其放入巨集中(不可能使用\verb
)。語法是:
\literalset\foo<SPACE>ARBITRARY CHARACTERS<END OF LINE>
<END OF LINE>
請注意,遺囑之前的輸入中的空格不被捕獲在宏觀上\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}