
2 つの列を持つテーブルを作成しようとしています。2 番目の列は、想像できるすべての文字を含む正規表現を多数表しています。\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}
アップデート
この回避策に感謝します。期待どおりに動作します。 tabular 環境ではなく tabularx を使用した理由は、正規表現セルに手動で行を分割する必要があったためです。このソリューションではまだ不可能のようですが、何もないよりはましです :)
答え1
@DavidCarlisle がコメントで既に示しているように、次のように進めることができます。tabular
環境の代わりに環境を使用しtabularx
、p
2 番目の列に列タイプを使用し、最初の列の幅に関する情報を使用してその幅を計算します。この設定により、マクロを使用して、\url
長い正規表現文字列を URL 文字列であるかのようにタイプセットできるようになります。
以下の例では、これらの文字が正規表現文字列に出現しても、正しい順序で出現し、バランスが取れているため、 {
and/or の}
使用は許可されています。そうでない場合、つまり中括弧がバランスが取れていない場合は、正規表現文字列のどこにも出現しない文字を使用する必要があります。たとえば、\url!...!
and は、正規表現文字列に もも出現しない\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
キャプチャするにはテキストに含まれていない 1 つの文字が必要です。には\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}