在下面的代码中,我想使用
\begin{tiScreen}
12345678901 234567890123456789
12345678901234567890123456789
\end{tiScreen}
代替
\begin{tiScreen}
\breakabletexttt{12345678901} \breakabletexttt{234567890123456789}
\breakabletexttt{12345678901234567890123456789}
\end{tiScreen}
我还需要使用 LaTeX 约定来保留空格:连续的空格显示为一个。
代码
\documentclass{article}
\usepackage{mdframed}
\newenvironment{tiScreen}{%
\ttfamily \begin{minipage}{9.5em}%
\begin{mdframed}[innerrightmargin=0.2cm, innerleftmargin=0.2cm]%
}{\end{mdframed} \end{minipage}}
\makeatletter
\newcommand\breakabletexttt{%
\begingroup
\catcode`\_=12 \catcode`\#=12
\@breakabletexttt
}
\newcommand\@breakabletexttt[1]{%
\ttfamily
\breakable@texttt#1\@nil%
\endgroup%
}
\def\@gobble@fi#1\fi{\fi#1}
\def\breakable@texttt#1#2\@nil{%
#1\hspace{0pt plus 0.1pt minus 0.1pt}%
\ifx\relax#2\relax
\else
\@gobble@fi\breakable@texttt#2\@nil
\fi
}
\makeatother
\begin{document}
\breakabletexttt{12345678901 234567890123456789}
\breakabletexttt{12345678901} \breakabletexttt{234567890123456789}
\breakabletexttt{12345678901234567890123456789}
\end{document}
答案1
这是一个以 unbonpetit 的解决方案为模型的解决方案,它尊重空格和换行符:
\documentclass{article}
\makeatletter
\newsavebox{\tiScreenbox}
\newenvironment{tiScreen}
{\begin{lrbox}{\tiScreenbox}\ttfamily\minipage{9em}
\catcode`\_=12 \catcode`\#=12 \obeyspaces \obeylines
\begingroup\lccode`~=` \lowercase{\endgroup\def~}{\unskip\space}%
\begingroup\lccode`~=` \lowercase{\endgroup\let\brt@space~}%
\begingroup\lccode`~=`\^^M \lowercase{\endgroup\def~}{\ifvmode\else\endgraf\fi}%
\breakabletexttt@i}
{\vspace{-\baselineskip}\endminipage\end{lrbox}\fbox{\usebox\tiScreenbox}}
\long\def\breakabletexttt@i#1{%
\ifx#1\end
\expandafter\breakabletexttt@ii
\else
\ifx#1\brt@space
\unskip\space
\else
#1\hskip\z@ plus.2em
\fi
\expandafter\breakabletexttt@i
\fi}
\def\breakabletexttt@ii#1{%
\end{#1}%
\def\temp@{#1}\def\temp@@{tiScreen}%
\ifx\temp@\temp@@\else
\expandafter\breakabletexttt@i
\fi}
\makeatother
\begin{document}
\begin{tiScreen}
1234567890123 4567890123456789
12345678901234567890123456789
\end{tiScreen}
\begin{tiScreen}
1234567890123 4567890123456789
123
12345678901234567890123456789
\end{tiScreen}
\end{document}
其优点是不会产生纸箱过满或欠满的情况。
答案2
\documentclass{article}
\usepackage{mdframed}
\makeatletter
\newenvironment{tiScreen}
{\ttfamily\minipage{9.5em}%
\mdframed[innerrightmargin=0.2cm, innerleftmargin=0.2cm]%
\catcode`\_=12 \catcode`\#=12 \breakabletexttt@i}
{\endmdframed\endminipage}
\long\def\breakabletexttt@i#1{%
\ifx#1\end
\expandafter\breakabletexttt@ii
\else
#1\hskip\z@ plus.1pt minus.1pt
\expandafter\breakabletexttt@i
\fi}
\def\breakabletexttt@ii#1{%
\end{#1}%
\def\temp@{#1}\def\temp@@{tiScreen}%
\ifx\temp@\temp@@\else
\expandafter\breakabletexttt@i
\fi}
\makeatother
\begin{document}
\begin{tiScreen}
12345678901234567890123456789
12345678901234567890123456789
\end{tiScreen}
\end{document}
如果您可以描述哪些标记可以出现在您的tiScreen
环境中,那么给出一个好的答案会更容易。此代码考虑了空格:
\documentclass{article}
\usepackage{mdframed}
\makeatletter
\newenvironment{tiScreen}
{\ttfamily\parindent\z@\minipage{9.5em}%
\mdframed[innerrightmargin=0.2cm, innerleftmargin=0.2cm]%
\catcode`\_=12 \catcode`\#=12 \let\temp@@\@currenvir
\testspc@}
{\endmdframed\endminipage}
\def\testspc@{\futurelet\nxt@toks\testspc@i}
\def\testspc@i{%
\ifx\nxt@toks\@sptoken\expandafter\@firstoftwo\else\expandafter\@secondoftwo\fi
{\afterassignment\testspc@\let\nxt@toks= }\breakabletexttt@i}
\def\breakabletexttt@i{\afterassignment\breakabletexttt@ii\let\nxt@toks= }
\def\breakabletexttt@ii{%
\ifx\nxt@toks\end
\expandafter\breakabletexttt@iii
\else
\nxt@toks\hskip\z@ plus.1pt minus.1pt
\expandafter\breakabletexttt@i
\fi}
\def\breakabletexttt@iii#1{%
\end{#1}\def\temp@{#1}%
\unless\ifx\temp@\temp@@\expandafter\breakabletexttt@i\fi}
\makeatother
\begin{document}
\begin{tiScreen}
123456789012 34567890123456789
12345678901234567890123456789
\end{tiScreen}
\end{document}