6

How can I frame a single sentence in a paragraph when the sentence may wrap around to become several lines long? Just like \color but to also provide background color, frame, styling, and so forth. The idea is to provide a formatting for inline code that unlike \type ignores input whitespace.

  • \color works fine but only sets the foreground color
  • \framed doesn't wrap
  • \type wraps but only sets the foreground color. Also unlike \color doesn't ignore input whitespace.
  • \start...stoptextbackground forces a new block even when used as left= and right= arguments to \setuptype.
  • \framed doesn't actually wrap the input to \type when used like:

    \setuptype[command=\mtc]
    \define[1]\mtc{%
        \dontleavehmode{\framed[frame=on,
                                background=color,
                                background-color=orange,
                               ]{#1}}}
    

This is what I mean by "ignore input whitespace":

\setuptype[color=red,style=\tf]
\starttext
    Lorem Ipsum is simply dummy text of the printing and typesetting industry.
    Lorem Ipsum has been \type{the industry's standard dummy text ever since the 1500s,
    when an unknown printer took a galley of type and scrambled it to make a type
    specimen book. It has survived not only five centuries}, but also the leap into
    electronic typesetting, remaining essentially unchanged. It was popularised in
    the 1960s with the release of Letraset sheets containing Lorem Ipsum passages,
    and more recently with desktop publishing software like Aldus PageMaker
    including versions of Lorem Ipsum.


    Lorem Ipsum is simply dummy text of the printing and typesetting industry.
    Lorem Ipsum has been \color[red]{the industry's standard dummy text ever since the 1500s,
    when an unknown printer took a galley of type and scrambled it to make a type
    specimen book. It has survived not only five centuries}, but also the leap into
    electronic typesetting, remaining essentially unchanged. It was popularised in
    the 1960s with the release of Letraset sheets containing Lorem Ipsum passages,
    and more recently with desktop publishing software like Aldus PageMaker
    including versions of Lorem Ipsum.
\stoptext

Clarify "ignore input whitespace". Notice the unintentional whitespace in the first paragraph.

Though I guess it doesn't matter since I can do this, based on the accepted answer:

\definetextbackground
  [mtcframed]
  [frame=on,
   framecolor=black,
   backgroundcolor=orange,
   rulethickness=1pt,
   location=text,
  ]

\starttext
    Lorem Ipsum is simply dummy text of the printing and typesetting industry.
    Lorem Ipsum has been \starttextbackground[mtcframed]the industry's standard dummy text ever since the 1500s,
    when an unknown printer took a galley of type and scrambled it to make a type
    specimen book. It has survived not only five centuries\stoptextbackground, but also the leap into
    electronic typesetting, remaining essentially unchanged. It was popularised in
    the 1960s with the release of Letraset sheets containing Lorem Ipsum passages,
    and more recently with desktop publishing software like Aldus PageMaker
    including versions of Lorem Ipsum.
\stoptext

Using <code>\starttextbackground</code> without <code>\type</code>, inline within a block

And then define a new macro for consistency with other style commands:

\define[1]\mtc{\starttextbackground[mtcframed]#1\stoptextbackground}

Edit:

There are minor overfull box issues in English as well. To be fair this is related only to the use of monotype fonts. Unfortunately it is very noticeable as the text spills out of the frame. How can I fix this? For example, with either approach:

\unprotect
\defineinterfacevariable{collapse}{collapse}
\setvalue{\??typingspace\v!collapse}{\def\obeyedspace{\unskip\space}}
\protect

\definetextbackground
  [mtcframed]
  [frame=on,
   framecolor=black,
   backgroundcolor=orange,
   rulethickness=1pt,
   location=text,
  ]

\define[1]\mtcA{\starttextbackground[mtcframed]\tt#1\stoptextbackground}

\definetype[mtcB]
    [space=collapse,
     left={\starttextbackground[mtcframed]},
     right={\stoptextbackground},
    ]

Demonstrate how, with either approach, text overflows the frame

Also is there any reason to prefer \definetype[...][space=collapse...] over define[1]\mtc{...}? The former is interesting and useful but seems overly complex.

  • Use \definetype[mtc][space=stretch] to get the regular interword glue. I have also updated my answer. – Henri Menke Dec 1 '18 at 6:44
  • For me it doesn't spill out the frame. Are you using the latest beta? – Henri Menke Dec 2 '18 at 7:58
  • The other problem is that hyphenation is by default switched off for teletype fonts, because you usually use those for code and hyphenation doesn't make sense there. – Henri Menke Dec 2 '18 at 8:00
  • Hyphenation is disabled for \type, but it doesn't seem by default switched off for teletype fonts. If I typeset my example paragraph using \tt I actually have more hyphenation than without. As for \type setting lines=hyphenated doesn't influence the overflow. – user19087 Dec 2 '18 at 15:34
  • I'm using the ConTeXt standalone beta 2018.10.18. – user19087 Dec 2 '18 at 15:34
9

\start...\stoptextbackground is the correct thing to do but you have to set location=text. To collapse interword spaces, I define a new spacing method collapse and use it in \setuptype. I also add a little bit of stretch and shrink to the interword space for better linebreaking.

% The original version, which only collapses interword spaces.
%\unprotect
%\defineinterfacevariable{collapse}{collapse}
%\setvalue{\??typingspace\v!collapse}{\def\obeyedspace{\unskip\space}}
%\protect

% The updated version, which in addition adds stretch and shrink
\unprotect
\defineinterfacevariable{collapse}{collapse}
\setvalue
  {\??typingspace\v!collapse}%
  {\def\obeyedspace{\unskip
      \hskip\interwordspace
      plus .5\interwordspace
      minus .5\interwordspace\relax}}
\protect

\definetype
  [mtc]
  [left={\starttextbackground[mtcframed]},
   right={\stoptextbackground},
   space=collapse]

\definetextbackground
  [mtcframed]
  [frame=on,
   framecolor=black,
   backgroundcolor=orange,
   rulethickness=1pt,
   location=text]

\starttext

Lorem Ipsum is simply dummy text of the printing and typesetting
industry.  Lorem Ipsum has been \mtc{the industry's standard dummy
  text ever since the 1500s, when an unknown printer took a galley of
  type and scrambled it to make a type specimen book. It has survived
  not only five centuries}, but also the leap into electronic
typesetting, remaining essentially unchanged. It was popularised in
the 1960s with the release of Letraset sheets containing Lorem Ipsum
passages, and more recently with desktop publishing software like
Aldus PageMaker including versions of Lorem Ipsum.

\stoptext

enter image description here

  • +1: Great answer. LaTeX (and the contributors here) keep to amaze me. – Dr. Manuel Kuehner Dec 5 '18 at 22:55
  • Why use \type? Isn't it better to simply set style=mono – Aditya Dec 6 '18 at 3:00
  • @Aditya In this case yes, but OP wants to use this to typeset code. – Henri Menke Dec 6 '18 at 3:09

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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