Align last word of paragraph to right with raggedright











up vote
2
down vote

favorite












I want to align the last word of a paragraph to the right like this example from The TeXbook.



This works great with a justified paragraph, but fails with a raggedright paragraph.



In case it's relevant, my specific example in the MWE example below includes a slightly more complicated paragraph setup for typesetting psalms based on code from this answer.



There are two issues with my current set up.




  1. The second to last word in the paragraph wraps on to the last line even when there is space for it on the previous line.

  2. The last word of the paragraph (Selah) is not set flush right.


I also want to avoid hyphenation.



documentclass{article}

usepackage{xparse}
usepackage[showframe,textwidth=5.6cm,textheight=10cm]{geometry}
parindent 0em

ExplSyntaxOn

dim_new:N l__scripture_indent_dim

dim_set:Nn l__scripture_indent_dim { 1em }

cs_new:Nn scripture_vs:n
{
textsuperscript {#1}
}

cs_new:Nn scripture_vs_overlap_left:n
{
hbox_overlap_left:n
{
scripture_vs:n {#1}
}
}

cs_new:Nn scripture_format_selah:n
{
emph {#1}
}

cs_new:Nn scripture_selah:
{
{
unskip
nobreak
hfil
penalty 50
skip_horizontal:N 2em
hbox:n {}
nobreak
hfil
scripture_format_selah:n { Selah }
parfillskip = 0pt
finalhyphendemerits = 0
endgraf
skip_vertical:n { -baselineskip }
leavevmode
}
}

cs_new:Nn scripture_vs_outdent_overlap_left:n
{
skip_horizontal:N -l__scripture_indent_dim
scripture_vs_overlap_left:n {#1}
skip_horizontal:N l__scripture_indent_dim
}

cs_new_protected:Nn scripture_psalm_par:
{
mode_if_vertical:TF
{
cs_set_eq:NN vs scripture_vs_overlap_left:n
noindent
}
{
cs_set_eq:NN vs scripture_vs_outdent_overlap_left:n
endgraf
}
dim_set:Nn hangindent { 4 l__scripture_indent_dim }
}

NewDocumentCommand selah { }
{
scripture_selah:
}

NewDocumentEnvironment {psalm} { }
{
raggedright
cs_set_eq:NN vs scripture_vs_overlap_left:n
cs_set_eq:NN par scripture_psalm_par:
dim_set_eq:NN leftskip l__scripture_indent_dim
dim_set_eq:NN parindent l__scripture_indent_dim
obeylines
}
{ }

ExplSyntaxOff

begin{document}

section*{What I get}

begin{psalm}
vs{1}textsc{Lord}, how many are my foes!
How many rise up against me!

vs{2}Many are saying of me,
‘God will not deliver him.’selahmedskip

vs{3}But you, textsc{Lord}, are a shield around me,
my glory, the One who lifts my head high.

vs{4}I call out to the Lord,
and he answers me from his holy mountain.selah
end{psalm}

section*{What I'd like}

hspace*{2em}`God will not deliver him.'selah

medskip

hspace*{2em}and he answers me from his \
hspace*{5em}holy mountain.selah

end{document}


output










share|improve this question


























    up vote
    2
    down vote

    favorite












    I want to align the last word of a paragraph to the right like this example from The TeXbook.



    This works great with a justified paragraph, but fails with a raggedright paragraph.



    In case it's relevant, my specific example in the MWE example below includes a slightly more complicated paragraph setup for typesetting psalms based on code from this answer.



    There are two issues with my current set up.




    1. The second to last word in the paragraph wraps on to the last line even when there is space for it on the previous line.

    2. The last word of the paragraph (Selah) is not set flush right.


    I also want to avoid hyphenation.



    documentclass{article}

    usepackage{xparse}
    usepackage[showframe,textwidth=5.6cm,textheight=10cm]{geometry}
    parindent 0em

    ExplSyntaxOn

    dim_new:N l__scripture_indent_dim

    dim_set:Nn l__scripture_indent_dim { 1em }

    cs_new:Nn scripture_vs:n
    {
    textsuperscript {#1}
    }

    cs_new:Nn scripture_vs_overlap_left:n
    {
    hbox_overlap_left:n
    {
    scripture_vs:n {#1}
    }
    }

    cs_new:Nn scripture_format_selah:n
    {
    emph {#1}
    }

    cs_new:Nn scripture_selah:
    {
    {
    unskip
    nobreak
    hfil
    penalty 50
    skip_horizontal:N 2em
    hbox:n {}
    nobreak
    hfil
    scripture_format_selah:n { Selah }
    parfillskip = 0pt
    finalhyphendemerits = 0
    endgraf
    skip_vertical:n { -baselineskip }
    leavevmode
    }
    }

    cs_new:Nn scripture_vs_outdent_overlap_left:n
    {
    skip_horizontal:N -l__scripture_indent_dim
    scripture_vs_overlap_left:n {#1}
    skip_horizontal:N l__scripture_indent_dim
    }

    cs_new_protected:Nn scripture_psalm_par:
    {
    mode_if_vertical:TF
    {
    cs_set_eq:NN vs scripture_vs_overlap_left:n
    noindent
    }
    {
    cs_set_eq:NN vs scripture_vs_outdent_overlap_left:n
    endgraf
    }
    dim_set:Nn hangindent { 4 l__scripture_indent_dim }
    }

    NewDocumentCommand selah { }
    {
    scripture_selah:
    }

    NewDocumentEnvironment {psalm} { }
    {
    raggedright
    cs_set_eq:NN vs scripture_vs_overlap_left:n
    cs_set_eq:NN par scripture_psalm_par:
    dim_set_eq:NN leftskip l__scripture_indent_dim
    dim_set_eq:NN parindent l__scripture_indent_dim
    obeylines
    }
    { }

    ExplSyntaxOff

    begin{document}

    section*{What I get}

    begin{psalm}
    vs{1}textsc{Lord}, how many are my foes!
    How many rise up against me!

    vs{2}Many are saying of me,
    ‘God will not deliver him.’selahmedskip

    vs{3}But you, textsc{Lord}, are a shield around me,
    my glory, the One who lifts my head high.

    vs{4}I call out to the Lord,
    and he answers me from his holy mountain.selah
    end{psalm}

    section*{What I'd like}

    hspace*{2em}`God will not deliver him.'selah

    medskip

    hspace*{2em}and he answers me from his \
    hspace*{5em}holy mountain.selah

    end{document}


    output










    share|improve this question
























      up vote
      2
      down vote

      favorite









      up vote
      2
      down vote

      favorite











      I want to align the last word of a paragraph to the right like this example from The TeXbook.



      This works great with a justified paragraph, but fails with a raggedright paragraph.



      In case it's relevant, my specific example in the MWE example below includes a slightly more complicated paragraph setup for typesetting psalms based on code from this answer.



      There are two issues with my current set up.




      1. The second to last word in the paragraph wraps on to the last line even when there is space for it on the previous line.

      2. The last word of the paragraph (Selah) is not set flush right.


      I also want to avoid hyphenation.



      documentclass{article}

      usepackage{xparse}
      usepackage[showframe,textwidth=5.6cm,textheight=10cm]{geometry}
      parindent 0em

      ExplSyntaxOn

      dim_new:N l__scripture_indent_dim

      dim_set:Nn l__scripture_indent_dim { 1em }

      cs_new:Nn scripture_vs:n
      {
      textsuperscript {#1}
      }

      cs_new:Nn scripture_vs_overlap_left:n
      {
      hbox_overlap_left:n
      {
      scripture_vs:n {#1}
      }
      }

      cs_new:Nn scripture_format_selah:n
      {
      emph {#1}
      }

      cs_new:Nn scripture_selah:
      {
      {
      unskip
      nobreak
      hfil
      penalty 50
      skip_horizontal:N 2em
      hbox:n {}
      nobreak
      hfil
      scripture_format_selah:n { Selah }
      parfillskip = 0pt
      finalhyphendemerits = 0
      endgraf
      skip_vertical:n { -baselineskip }
      leavevmode
      }
      }

      cs_new:Nn scripture_vs_outdent_overlap_left:n
      {
      skip_horizontal:N -l__scripture_indent_dim
      scripture_vs_overlap_left:n {#1}
      skip_horizontal:N l__scripture_indent_dim
      }

      cs_new_protected:Nn scripture_psalm_par:
      {
      mode_if_vertical:TF
      {
      cs_set_eq:NN vs scripture_vs_overlap_left:n
      noindent
      }
      {
      cs_set_eq:NN vs scripture_vs_outdent_overlap_left:n
      endgraf
      }
      dim_set:Nn hangindent { 4 l__scripture_indent_dim }
      }

      NewDocumentCommand selah { }
      {
      scripture_selah:
      }

      NewDocumentEnvironment {psalm} { }
      {
      raggedright
      cs_set_eq:NN vs scripture_vs_overlap_left:n
      cs_set_eq:NN par scripture_psalm_par:
      dim_set_eq:NN leftskip l__scripture_indent_dim
      dim_set_eq:NN parindent l__scripture_indent_dim
      obeylines
      }
      { }

      ExplSyntaxOff

      begin{document}

      section*{What I get}

      begin{psalm}
      vs{1}textsc{Lord}, how many are my foes!
      How many rise up against me!

      vs{2}Many are saying of me,
      ‘God will not deliver him.’selahmedskip

      vs{3}But you, textsc{Lord}, are a shield around me,
      my glory, the One who lifts my head high.

      vs{4}I call out to the Lord,
      and he answers me from his holy mountain.selah
      end{psalm}

      section*{What I'd like}

      hspace*{2em}`God will not deliver him.'selah

      medskip

      hspace*{2em}and he answers me from his \
      hspace*{5em}holy mountain.selah

      end{document}


      output










      share|improve this question













      I want to align the last word of a paragraph to the right like this example from The TeXbook.



      This works great with a justified paragraph, but fails with a raggedright paragraph.



      In case it's relevant, my specific example in the MWE example below includes a slightly more complicated paragraph setup for typesetting psalms based on code from this answer.



      There are two issues with my current set up.




      1. The second to last word in the paragraph wraps on to the last line even when there is space for it on the previous line.

      2. The last word of the paragraph (Selah) is not set flush right.


      I also want to avoid hyphenation.



      documentclass{article}

      usepackage{xparse}
      usepackage[showframe,textwidth=5.6cm,textheight=10cm]{geometry}
      parindent 0em

      ExplSyntaxOn

      dim_new:N l__scripture_indent_dim

      dim_set:Nn l__scripture_indent_dim { 1em }

      cs_new:Nn scripture_vs:n
      {
      textsuperscript {#1}
      }

      cs_new:Nn scripture_vs_overlap_left:n
      {
      hbox_overlap_left:n
      {
      scripture_vs:n {#1}
      }
      }

      cs_new:Nn scripture_format_selah:n
      {
      emph {#1}
      }

      cs_new:Nn scripture_selah:
      {
      {
      unskip
      nobreak
      hfil
      penalty 50
      skip_horizontal:N 2em
      hbox:n {}
      nobreak
      hfil
      scripture_format_selah:n { Selah }
      parfillskip = 0pt
      finalhyphendemerits = 0
      endgraf
      skip_vertical:n { -baselineskip }
      leavevmode
      }
      }

      cs_new:Nn scripture_vs_outdent_overlap_left:n
      {
      skip_horizontal:N -l__scripture_indent_dim
      scripture_vs_overlap_left:n {#1}
      skip_horizontal:N l__scripture_indent_dim
      }

      cs_new_protected:Nn scripture_psalm_par:
      {
      mode_if_vertical:TF
      {
      cs_set_eq:NN vs scripture_vs_overlap_left:n
      noindent
      }
      {
      cs_set_eq:NN vs scripture_vs_outdent_overlap_left:n
      endgraf
      }
      dim_set:Nn hangindent { 4 l__scripture_indent_dim }
      }

      NewDocumentCommand selah { }
      {
      scripture_selah:
      }

      NewDocumentEnvironment {psalm} { }
      {
      raggedright
      cs_set_eq:NN vs scripture_vs_overlap_left:n
      cs_set_eq:NN par scripture_psalm_par:
      dim_set_eq:NN leftskip l__scripture_indent_dim
      dim_set_eq:NN parindent l__scripture_indent_dim
      obeylines
      }
      { }

      ExplSyntaxOff

      begin{document}

      section*{What I get}

      begin{psalm}
      vs{1}textsc{Lord}, how many are my foes!
      How many rise up against me!

      vs{2}Many are saying of me,
      ‘God will not deliver him.’selahmedskip

      vs{3}But you, textsc{Lord}, are a shield around me,
      my glory, the One who lifts my head high.

      vs{4}I call out to the Lord,
      and he answers me from his holy mountain.selah
      end{psalm}

      section*{What I'd like}

      hspace*{2em}`God will not deliver him.'selah

      medskip

      hspace*{2em}and he answers me from his \
      hspace*{5em}holy mountain.selah

      end{document}


      output







      horizontal-alignment






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked 1 hour ago









      David Purton

      8,5911834




      8,5911834






















          1 Answer
          1






          active

          oldest

          votes

















          up vote
          1
          down vote













          Looks like you can slightly modify the scripture_selah definition. I added the hspace*{0.025textwidth} just to make sure there is some extra space, so that Selah is padded and not immediately flush (if there is enough space, e.g. with him.').



          cs_new:Nn scripture_selah:
          {
          {
          unskip
          hfil
          penalty 50
          hbox:n {}
          nobreak
          hspace*{fill}
          scripture_format_selah:n { hspace*{0.025textwidth}Selah }
          parfillskip = 0pt
          finalhyphendemerits = 0
          endgraf
          skip_vertical:n { -baselineskip }
          leavevmode
          }
          }


          modified horizontal formatting



          This works in the particular example that you provided. I am not sure if this will robustly work in every situation that you will be using it in.






          share|improve this answer





















            Your Answer








            StackExchange.ready(function() {
            var channelOptions = {
            tags: "".split(" "),
            id: "85"
            };
            initTagRenderer("".split(" "), "".split(" "), channelOptions);

            StackExchange.using("externalEditor", function() {
            // Have to fire editor after snippets, if snippets enabled
            if (StackExchange.settings.snippets.snippetsEnabled) {
            StackExchange.using("snippets", function() {
            createEditor();
            });
            }
            else {
            createEditor();
            }
            });

            function createEditor() {
            StackExchange.prepareEditor({
            heartbeatType: 'answer',
            convertImagesToLinks: false,
            noModals: true,
            showLowRepImageUploadWarning: true,
            reputationToPostImages: null,
            bindNavPrevention: true,
            postfix: "",
            imageUploader: {
            brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
            contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
            allowUrls: true
            },
            onDemand: true,
            discardSelector: ".discard-answer"
            ,immediatelyShowMarkdownHelp:true
            });


            }
            });














            draft saved

            draft discarded


















            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2ftex.stackexchange.com%2fquestions%2f466306%2falign-last-word-of-paragraph-to-right-with-raggedright%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown

























            1 Answer
            1






            active

            oldest

            votes








            1 Answer
            1






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes








            up vote
            1
            down vote













            Looks like you can slightly modify the scripture_selah definition. I added the hspace*{0.025textwidth} just to make sure there is some extra space, so that Selah is padded and not immediately flush (if there is enough space, e.g. with him.').



            cs_new:Nn scripture_selah:
            {
            {
            unskip
            hfil
            penalty 50
            hbox:n {}
            nobreak
            hspace*{fill}
            scripture_format_selah:n { hspace*{0.025textwidth}Selah }
            parfillskip = 0pt
            finalhyphendemerits = 0
            endgraf
            skip_vertical:n { -baselineskip }
            leavevmode
            }
            }


            modified horizontal formatting



            This works in the particular example that you provided. I am not sure if this will robustly work in every situation that you will be using it in.






            share|improve this answer

























              up vote
              1
              down vote













              Looks like you can slightly modify the scripture_selah definition. I added the hspace*{0.025textwidth} just to make sure there is some extra space, so that Selah is padded and not immediately flush (if there is enough space, e.g. with him.').



              cs_new:Nn scripture_selah:
              {
              {
              unskip
              hfil
              penalty 50
              hbox:n {}
              nobreak
              hspace*{fill}
              scripture_format_selah:n { hspace*{0.025textwidth}Selah }
              parfillskip = 0pt
              finalhyphendemerits = 0
              endgraf
              skip_vertical:n { -baselineskip }
              leavevmode
              }
              }


              modified horizontal formatting



              This works in the particular example that you provided. I am not sure if this will robustly work in every situation that you will be using it in.






              share|improve this answer























                up vote
                1
                down vote










                up vote
                1
                down vote









                Looks like you can slightly modify the scripture_selah definition. I added the hspace*{0.025textwidth} just to make sure there is some extra space, so that Selah is padded and not immediately flush (if there is enough space, e.g. with him.').



                cs_new:Nn scripture_selah:
                {
                {
                unskip
                hfil
                penalty 50
                hbox:n {}
                nobreak
                hspace*{fill}
                scripture_format_selah:n { hspace*{0.025textwidth}Selah }
                parfillskip = 0pt
                finalhyphendemerits = 0
                endgraf
                skip_vertical:n { -baselineskip }
                leavevmode
                }
                }


                modified horizontal formatting



                This works in the particular example that you provided. I am not sure if this will robustly work in every situation that you will be using it in.






                share|improve this answer












                Looks like you can slightly modify the scripture_selah definition. I added the hspace*{0.025textwidth} just to make sure there is some extra space, so that Selah is padded and not immediately flush (if there is enough space, e.g. with him.').



                cs_new:Nn scripture_selah:
                {
                {
                unskip
                hfil
                penalty 50
                hbox:n {}
                nobreak
                hspace*{fill}
                scripture_format_selah:n { hspace*{0.025textwidth}Selah }
                parfillskip = 0pt
                finalhyphendemerits = 0
                endgraf
                skip_vertical:n { -baselineskip }
                leavevmode
                }
                }


                modified horizontal formatting



                This works in the particular example that you provided. I am not sure if this will robustly work in every situation that you will be using it in.







                share|improve this answer












                share|improve this answer



                share|improve this answer










                answered 1 hour ago









                whatisit

                642313




                642313






























                    draft saved

                    draft discarded




















































                    Thanks for contributing an answer to TeX - LaTeX Stack Exchange!


                    • Please be sure to answer the question. Provide details and share your research!

                    But avoid



                    • Asking for help, clarification, or responding to other answers.

                    • Making statements based on opinion; back them up with references or personal experience.


                    To learn more, see our tips on writing great answers.





                    Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


                    Please pay close attention to the following guidance:


                    • Please be sure to answer the question. Provide details and share your research!

                    But avoid



                    • Asking for help, clarification, or responding to other answers.

                    • Making statements based on opinion; back them up with references or personal experience.


                    To learn more, see our tips on writing great answers.




                    draft saved


                    draft discarded














                    StackExchange.ready(
                    function () {
                    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2ftex.stackexchange.com%2fquestions%2f466306%2falign-last-word-of-paragraph-to-right-with-raggedright%23new-answer', 'question_page');
                    }
                    );

                    Post as a guest















                    Required, but never shown





















































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown

































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown







                    Popular posts from this blog

                    Contact image not getting when fetch all contact list from iPhone by CNContact

                    count number of partitions of a set with n elements into k subsets

                    A CLEAN and SIMPLE way to add appendices to Table of Contents and bookmarks