
저는 두 개의 열이 있는 테이블을 만들려고 합니다. 두 번째 열은 여러분이 상상할 수 있는 모든 문자가 포함된 많은 정규식을 나타냅니다. 나는 \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
두 번째 열의 열 유형을 사용하고, 첫 번째 열의 너비에 대한 정보를 사용하여 너비를 계산합니다. 이 설정을 사용하면 \url
매크로를 사용하여 긴 정규식 문자열을 마치 URL 문자열인 것처럼 조판할 수 있습니다.
아래 예에서는 이러한 문자가 정규식 문자열에 나타나더라도 올바른 순서로 발생하고 균형이 잡혀 있기 때문에 {
및/또는 사용이 허용됩니다. }
그렇지 않은 경우, 즉 중괄호의 균형이 맞지 않으면 정규식 문자열의 어느 곳에서도 나타나지 않는 문자를 사용해야 합니다. 예를 들어, 정규식 문자열에는 둘 다 발생 하지 않으므로 둘 다 작동합니다 \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
균형 잡힌 텍스트가 필요합니다 .
캡처 축어 매크로를 세계에 발표
아이디어는 임의의 문자를 캡처하고(그러나 표준 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}