Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

In the following code, I would like to use

\begin{tiScreen}
12345678901 234567890123456789

12345678901234567890123456789
\end{tiScreen}

instead of

\begin{tiScreen}
\breakabletexttt{12345678901} \breakabletexttt{234567890123456789}

\breakabletexttt{12345678901234567890123456789}
\end{tiScreen}

I also need to keep spaces using LaTeX convention : consecutive spaces are displayed as one.

The code

\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}
share|improve this question

2 Answers 2

up vote 1 down vote accepted

Here's a solution modelled on unbonpetit's one, which respect spaces and also newlines:

\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}

It has the advantage of not producing overfull and underfull boxes.

share|improve this answer
    
Thanks for the patch... :-) –  projetmbc Dec 8 '11 at 16:53
\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}

if youy could describe what tokens can appear in your tiScreen environment, it would be easier to give a good aswer. This code takes spaces into account:

\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}
share|improve this answer
    
You can also use mdframed without minipage: \newenvironment{tiScreen} {\ttfamily% \mdframed[innerrightmargin=0.2cm, innerleftmargin=0.2cm,nobreak,userdefinedwidth=9.5em]% \catcode_=12 \catcode`\#=12 \breakabletexttt@i} {\endmdframed}` –  Marco Daniel Dec 8 '11 at 12:42
    
@unbonpetit : thanks ! –  projetmbc Dec 8 '11 at 13:27
    
@unbonpetit : there's one unwanted feature. The spaces are gobbled by the environment and I don't want that. –  projetmbc Dec 8 '11 at 14:20

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.