Difference between revisions of "Wrapping"

From Wiki
Jump to navigation Jump to search
(Create page)
 
(Cropped examples added further information provided by Rik offlist)
Line 137: Line 137:
 
  [characters=all,
 
  [characters=all,
 
   alternative=dna]
 
   alternative=dna]
 
\starttext
 
  
 
\startframedtext[width=6cm,style=mono]
 
\startframedtext[width=6cm,style=mono]
Line 145: Line 143:
 
  GATTGCTTACTCCTGGTTGGTGGGGCTTACATTCTGTCGCCTCAAAACTACTAGAGCCGGCATATTCTAGAAGGGCCGCCTTCATGTGG
 
  GATTGCTTACTCCTGGTTGGTGGGGCTTACATTCTGTCGCCTCAAAACTACTAGAGCCGGCATATTCTAGAAGGGCCGCCTTCATGTGG
 
\stopframedtext
 
\stopframedtext
 
\stoptext
 
 
</context>
 
</context>
  
And a solution using a bit of plain TeX by Rik:
+
And a solution using {{cmd|handletokens}} by Rik:
 
<context source="yes">
 
<context source="yes">
 
\define[1]\DNA{\handletokens #1\with\DNAspacer}
 
\define[1]\DNA{\handletokens #1\with\DNAspacer}
 
\define[1]\DNAspacer{#1\hskip 2.3pt plus .1pt}
 
\define[1]\DNAspacer{#1\hskip 2.3pt plus .1pt}
  
\starttext
 
  
 
\startframedtext[width=6cm,style=mono]
 
\startframedtext[width=6cm,style=mono]
Line 160: Line 155:
 
\stopframedtext
 
\stopframedtext
  
\stoptext
 
 
</context>
 
</context>
  
 +
One caveat, however: this method always adds the spacer value, and can result in a blank line at the end in some cases, even when the spacer value is zero. This is not the case with the lua mechanism.
  
 
== See also ==
 
== See also ==

Revision as of 07:35, 25 April 2020

Very long continuous strings (such as SHA512 keys or DNA sequences) might have to be broken after any character irrespective of the current hyphenation scheme.

Example SHA512

This is Hans' trick from the list for SHA512 keys

\startluacode

     -- local shared = {
     --     start  = 1,
     --     length = 1,
     --     left   = false,
     --     right  = false,
     -- }

     local shared = {
         start  = 1,
         length = 1,
         before = utf.char(0xB7),
         after  = nil,
         left   = false,
         right  = false,
     }

     -- languages.hyphenators.traditional.installmethod("sha",
     --     function(dictionary,word,n)
     --         local t = { }
     --         for i=1,#word do
     --             t[i] = shared
     --         end
     --         return t
     --     end
     -- )

     -- or more efficient when used often:

     -- local all = { }
     -- for i=1,512 do
     --     all[i] = shared
     -- end
     -- languages.hyphenators.traditional.installmethod("sha",
     --     function(dictionary,word,n)
     --         return all
     --     end
     -- )

     -- or more obscure:

     -- local all = table.setmetatableindex({ }, function(t,k)
     --     t[k] = shared
     --     return shared
     -- end)
     --
     -- languages.hyphenators.traditional.installmethod("sha",
     --     function(dictionary,word,n)
     --         return all
     --     end
     -- )

     -- or just (lua is fast enough anyway)

     local all = table.setmetatableindex({ }, function(t,k)
         return shared
     end)

     languages.hyphenators.traditional.installmethod("sha",
         function(dictionary,word,n)
             return all
         end
     )
\stopluacode

\definehyphenationfeatures
   [sha]
   [characters=all,
    alternative=sha]

% \unexpanded\def\sha#1%
%   {\begingroup
%    \sethyphenationfeatures[sha]%
%    #1%
%    \endgroup}
%
% \setuphyphenation[method=traditional]

\unexpanded\def\sha#1%
   {\begingroup
    \sethyphenationfeatures[sha]%
    \setuphyphenation[method=traditional]%
    #1%
    \endgroup}

\showframe

\starttext

\setupalign[tolerant,stretch]

\dorecurse {10} {%
     some sha
     \sha{8b2f3c087046c3943ace0dc4f958ef2138e58a51b40e%
ef6fab6fa1aeb845cc257a410ab1b914bc399b4293f%
31c76fc2c73e5be5ea4d329f9e6820984688efec2} and
}

\stoptext

Example DNA sequences

This is an adoption from Wolfang using Lua:

\startluacode

    local shared = {
        start  = 1,
        length = 1,
        before = nil,
        after  = nil,
        left   = false,
        right  = false,
    }

    local all = table.setmetatableindex({ }, function(t,k)
        return shared
    end)

    languages.hyphenators.traditional.installmethod("dna",
        function(dictionary,word,n)
            return all
        end
    )
\stopluacode

\definehyphenationfeatures
 [dna]
 [characters=all,
  alternative=dna]

\startframedtext[width=6cm,style=mono]
 \sethyphenationfeatures[dna]
 \setuphyphenation[method=traditional]
 GATTGCTTACTCCTGGTTGGTGGGGCTTACATTCTGTCGCCTCAAAACTACTAGAGCCGGCATATTCTAGAAGGGCCGCCTTCATGTGG
\stopframedtext

And a solution using \handletokens by Rik:

\define[1]\DNA{\handletokens #1\with\DNAspacer}
\define[1]\DNAspacer{#1\hskip 2.3pt plus .1pt}


\startframedtext[width=6cm,style=mono]
\DNA{GATTGCTTACTCCTGGTTGGTGGGGCTTACATTCTGTCGCCTCAAAACTACTAGAGCCGGCATATTCTAGAAGGGCCGCCTTCATGTGG}
\stopframedtext

One caveat, however: this method always adds the spacer value, and can result in a blank line at the end in some cases, even when the spacer value is zero. This is not the case with the lua mechanism.

See also

Help from ConTeXt-Mailinglist/Forum

All issues with: