The module "txt_justify" of the Mastrave modelling library


Daniele de Rigo


Copyright and license notice of the function txt_justify



Copyright © 2005,2006,2007,2008,2009,2010,2011,2013 Daniele de Rigo

The file txt_justify.m is part of Mastrave.

Mastrave is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your options) any later version.

Mastrave is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.

You should have received a copy of the GNU General Public License along with Mastrave. If not, see

Function declaration



[justified_txt, jtc] = txt_justify( txt             ,
                                    max_length      ,
                                    options    = {} ,
                                    indent     = 0  )




Utility to justify the string txt . It ensures that the processed string has a maximum line-length of max_length characters and performs a customized transformation that can be set by configuring options . The argument indent specifies the number of indentation spaces. The first returned output argument is the justified string justified_txt . The optional second output argument is the cell-array jtc , each cell of it contains a line of the justified text.

Input arguments



 txt         ::string::
             Character array.

 max_length  ::scalar_natural_nonzero::
             Maximum number of character per line.

 options     ::cellstring::
             Set the behaviour of the text transformation.
             Valid options are:

                   option              meaning
                 'left'              Return respectively a 
                 'center'            left-aligned  | middle-aligned |
                 'right'             right-aligned | justified
                 'justified'         version of  txt .
                 'un_wordwrap'       Erase all spaces inside empty lines
                                     (lines filled with spaces only) and
                                     removes all single line terminators
                                     leaving untouched all consecutively
                                     repeated line terminators.
                 'unique_spaced'     Remove all duplicated spaces, and
                                     all starting and ending spaces
                                     leaving untouched the other "blank"
                                     characters ( \t, \n, \r, ...).
                 'sentence_spacing'  Add an extra space after full stops
                                     terminating sentences.

             If omitted, the default value is 'justified'.
             If multiple option-strings need to be invoked, they must
             be passed as elements of a cell-array of strings (an empty cell
             array is interpreted as if it were { 'justified' } ).

 indent      ::scalar_numel|row_string::
             Number of spaces to be prefixed to each line.  If a row-string
             is passed, the characters of  indent  are used instead of
             spaces.  Default value: 0.

Example of usage



   % Basic text manipulation
   s = [  'Lorem ipsum dolor sit amet, consectetur adipisici elit, sed '  ...
          'eiusmod tempor incidunt ut labore et dolore magna aliqua. Ut ' ...
          'enim ad minim veniam, quis nostrud exercitation ullamco '      ...
          'laboris nisi ut aliquid ex ea commodi consequat. Quis aute '   ...
          'iure reprehenderit in voluptate velit esse cillum dolore eu '  ...
          'fugiat nulla pariatur. Excepteur sint obcaecat cupiditat non ' ...
          'proident, sunt in culpa qui officia deserunt mollit anim id '  ...
          'est laborum.'  ];

   [js c] = txt_justify( s, 50)
   [js c] = txt_justify( s, 50,  'justified'  )
   [js c] = txt_justify( s, 50,  'right'      )
   [js c] = txt_justify( s, 50,  'center'     )
   [js c] = txt_justify( s, 50,  'left'       )

   % Matrix strings or even multidimensional-array strings
   % are supported
   s2d    = char( c )
   size( s2d )
   [js c] = txt_justify( s2d, 50,  'left'      )
   s3d(:,:,1) = char( c(1:3) );
   s3d(:,:,2) = char( c(4:6) );
   s3d(:,:,3) = char( c(7:9) );
   size( s3d )
   [js c] = txt_justify( s3d, 50,  'left'      )

   % The  indent  argument provides indentation and prefix support
   [js c] = txt_justify( s, 50,  'left'       , 3    )
   [js c] = txt_justify( s, 50,  'left'       , '| ' )

   % Advanced text manipulation
   wordwrapped = sprintf([  'Lorem ipsum dolor sit amet, consectetur\n'   ...
                            'adipisici elit, sed eiusmod tempor\n'        ...
                            'incidunt ut labore et dolore magna\n'        ...
                            'aliqua.   Ut enim ad minim veniam, quis\n'   ...
                            'nostrud exercitation ullamco laboris\n'      ...
                            'nisi ut aliquid ex ea commodi consequat.\n'  ...
                            '\n'                                          ...
                            '   Quis aute iure reprehenderit in\n'        ...
                            '   voluptate velit esse cillum dolore eu\n'  ...
                            '   fugiat nulla pariatur.\n'                 ...
                            '     \n'                                     ...
                            'Excepteur       sint\n'                      ...
                            'obcaecat cupiditat non proident, sunt in\n'  ...
                            'culpa qui officia deserunt mollit anim\n'    ...
                            'id est laborum.'                           ])

    [js c] = txt_justify( wordwrapped, 50,  'left'  )
    [js c] = txt_justify( wordwrapped, 50,  'right'  )
    [js c] = txt_justify( wordwrapped, 50,  'center'  )
    [js c] = txt_justify( wordwrapped, 50,  'justified'  )
    [js c] = txt_justify( wordwrapped, 50,  'un_wordwrap'  )
    [js c] = txt_justify( wordwrapped, 50,  'un_wordwrap'  , '| ' )
    [js c] = txt_justify( wordwrapped, 50,  {  'un_wordwrap'  'justified'  } )
    [js c] = txt_justify( js, 40,  {  'un_wordwrap'  'unique_spaced'  } )
    [js c] = txt_justify( js, 40, ...
        {  'un_wordwrap'  'unique_spaced'  'sentence_spacing'  } )
    [js c] = txt_justify( js, 40, ...
        {  'un_wordwrap'  'unique_spaced'  'justified'  } )

See also:

   text manipulation

Version: 0.6.5




The Mastrave modelling library is committed to provide reusable and general - but also robust and scalable - modules for research modellers dealing with computational science.  You can help the Mastrave project by providing feedbacks on unexpected behaviours of this module.  Despite all efforts, all of us - either developers or users - (should) know that errors are unavoidable.  However, the free software paradigm successfully highlights that scientific knowledge freedom also implies an impressive opportunity for collectively evolve the tools and ideas upon which our daily work is based.  Reporting a problem that you found using Mastrave may help the developer team to find a possible bug.  Please, be aware that Mastrave is entirely based on voluntary efforts: in order for your help to be as effective as possible, please read carefully the section on reporting problems.  Thank you for your collaboration.

Copyright (C) 2005, 2006, 2007, 2008, 2009, 2010, 2011, 2012, 2013, 2014, 2015, 2016 Daniele de Rigo

This page is licensed under a Creative Commons Attribution-NoDerivs 3.0 Italy License.

This document is also part of the book:
de Rigo, D. (2012). Semantic Array Programming with Mastrave - Introduction to Semantic Computational Modelling.

Valid XHTML 1.0 Transitional