How can we conditionally insert a line-break at the beginning of some text?












0















The Short Story



If some text won't all fit on the current line, we want all of that text moved to the next line instead. How can we do this?



The Medium-Length Story



Suppose we have a string of text, "banana banana." Having the first few characters of "banana [...]" on the current line and the second half of the banana string the next line is undesirable.



If the entire string will fit on the current line, then good, put the string on the current line. Otherwise put the string on the next line.



Undesirable:



apples apples apples banana
banana. cantaloupe cantaloupe


Desirable:



% a line break is inserted at the beginning
% of the bananas because they won't all fit
% on the current line.

apples apples apples
banana banana. cantaloupe cantaloupe


How can we write LaTeX code which makes this happen?



The Long Story



Consider the following test file. It will not compile because "KEEP TOGETHER" is not yet defined. The question can be viewed as: What do we need to do the make the following file produce the desired behavior?



documentclass{minimal}
begin{document}

apples apples apples apples
apples apples apples apples
apples apples apples apples

begin{KEEP TOGETHER}
Banana tincidunt ante banana.
end{KEEP TOGETHER}

cantaloupe cantaloupe cantaloupe
cantaloupe cantaloupe cantaloupe
cantaloupe cantaloupe.

end{document}


Suppose that STRYNG is the text block enclosed by begin{KEEP TOGETHER} and end{KEEP TOGETHER}. We wish to have the following behavior:



SPACER = four character indent based on
current font size and other settings.

if (STRYNG will fit on what remains
of the current line) {
WILL_FIT_HERE = true;
}
if (STRYNG will fit on an almost empty
blank line, SPACER at the beginning) {
WILL_FIT_NEXT = true;
}
if [(not WILL_FIT_HERE) and (WILL_FIT_NEXT)] {
print a line break
print SPACER on the brand new empty line.
print all of STRYNG after the indent
print a line-break.
else {
print STRYNG in the output document as if STRYNG
were not inside of a "KEEP TOGETHER" block.

Exhibit the same behavior as if the
lines begin{KEEP TOGETHER} and
end{KEEP TOGETHER} were commented out.

This probably means that STRYNG (or at least
the beginning of string) will be printed on
the current line.
}


Similar, but Different, Stack Exchange Questions



There is a question which embodies the same sentiments as the current question, but that question was much too vague. It was not clear what was wanted. Some people interpreted the question to mean, "how can we prevent a line break in the middle of a word?" They thought it was okay to have half of the string on on the current line and the second half on the next line. There were other interpretations as well.



The asker of prevent-line-break-in-a-span-of-text was interested in displaying URLs containing no space characters. Like me, they want a line break inserted at the very beginning of their text, instead of in the middle. However, they do not describe what behavior they want if the URL cannot all fit on one line. In the question you are reading, I do describe what behavior is desired in that instance. Also, for the other asker, maybe LaTeX was inserting line break into their urls before the right-margin was reached. Maybe they wanted characters in the URL to run all the way to the right-margin, and only then have a line break inserted then, but not any earlier than that. I am not sure what they wanted.



Unacceptable Solutions



Unacceptable: Always put a line break at the beginning



always inserting a line-break looks funny if there was plenty of space on the line above to fit the text you want to keep together.



Unacceptable: Non-breaking spaces (~)



Writing things like Cras~mattis~tincidunt~ante is not okay for a few reasons. For one, ~ must be inserted everywhere, which is a nuisance. Another is that the string to be "kept together," might not contain any space characters, won't all fit on the current line, but could fit on a new line all by itself. We want something similar to begin{command}{BLOCK OF TEXT}end{command}.



Unacceptable: nolinebreak



nolinebreak works once EXACTLY where you put it and then is not in effect after that point. Inserting nolinebreak in a dozen different places would make the source code unreadable. The issue is similar to using ~.









share







New contributor




IdleCustard is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.

























    0















    The Short Story



    If some text won't all fit on the current line, we want all of that text moved to the next line instead. How can we do this?



    The Medium-Length Story



    Suppose we have a string of text, "banana banana." Having the first few characters of "banana [...]" on the current line and the second half of the banana string the next line is undesirable.



    If the entire string will fit on the current line, then good, put the string on the current line. Otherwise put the string on the next line.



    Undesirable:



    apples apples apples banana
    banana. cantaloupe cantaloupe


    Desirable:



    % a line break is inserted at the beginning
    % of the bananas because they won't all fit
    % on the current line.

    apples apples apples
    banana banana. cantaloupe cantaloupe


    How can we write LaTeX code which makes this happen?



    The Long Story



    Consider the following test file. It will not compile because "KEEP TOGETHER" is not yet defined. The question can be viewed as: What do we need to do the make the following file produce the desired behavior?



    documentclass{minimal}
    begin{document}

    apples apples apples apples
    apples apples apples apples
    apples apples apples apples

    begin{KEEP TOGETHER}
    Banana tincidunt ante banana.
    end{KEEP TOGETHER}

    cantaloupe cantaloupe cantaloupe
    cantaloupe cantaloupe cantaloupe
    cantaloupe cantaloupe.

    end{document}


    Suppose that STRYNG is the text block enclosed by begin{KEEP TOGETHER} and end{KEEP TOGETHER}. We wish to have the following behavior:



    SPACER = four character indent based on
    current font size and other settings.

    if (STRYNG will fit on what remains
    of the current line) {
    WILL_FIT_HERE = true;
    }
    if (STRYNG will fit on an almost empty
    blank line, SPACER at the beginning) {
    WILL_FIT_NEXT = true;
    }
    if [(not WILL_FIT_HERE) and (WILL_FIT_NEXT)] {
    print a line break
    print SPACER on the brand new empty line.
    print all of STRYNG after the indent
    print a line-break.
    else {
    print STRYNG in the output document as if STRYNG
    were not inside of a "KEEP TOGETHER" block.

    Exhibit the same behavior as if the
    lines begin{KEEP TOGETHER} and
    end{KEEP TOGETHER} were commented out.

    This probably means that STRYNG (or at least
    the beginning of string) will be printed on
    the current line.
    }


    Similar, but Different, Stack Exchange Questions



    There is a question which embodies the same sentiments as the current question, but that question was much too vague. It was not clear what was wanted. Some people interpreted the question to mean, "how can we prevent a line break in the middle of a word?" They thought it was okay to have half of the string on on the current line and the second half on the next line. There were other interpretations as well.



    The asker of prevent-line-break-in-a-span-of-text was interested in displaying URLs containing no space characters. Like me, they want a line break inserted at the very beginning of their text, instead of in the middle. However, they do not describe what behavior they want if the URL cannot all fit on one line. In the question you are reading, I do describe what behavior is desired in that instance. Also, for the other asker, maybe LaTeX was inserting line break into their urls before the right-margin was reached. Maybe they wanted characters in the URL to run all the way to the right-margin, and only then have a line break inserted then, but not any earlier than that. I am not sure what they wanted.



    Unacceptable Solutions



    Unacceptable: Always put a line break at the beginning



    always inserting a line-break looks funny if there was plenty of space on the line above to fit the text you want to keep together.



    Unacceptable: Non-breaking spaces (~)



    Writing things like Cras~mattis~tincidunt~ante is not okay for a few reasons. For one, ~ must be inserted everywhere, which is a nuisance. Another is that the string to be "kept together," might not contain any space characters, won't all fit on the current line, but could fit on a new line all by itself. We want something similar to begin{command}{BLOCK OF TEXT}end{command}.



    Unacceptable: nolinebreak



    nolinebreak works once EXACTLY where you put it and then is not in effect after that point. Inserting nolinebreak in a dozen different places would make the source code unreadable. The issue is similar to using ~.









    share







    New contributor




    IdleCustard is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
    Check out our Code of Conduct.























      0












      0








      0








      The Short Story



      If some text won't all fit on the current line, we want all of that text moved to the next line instead. How can we do this?



      The Medium-Length Story



      Suppose we have a string of text, "banana banana." Having the first few characters of "banana [...]" on the current line and the second half of the banana string the next line is undesirable.



      If the entire string will fit on the current line, then good, put the string on the current line. Otherwise put the string on the next line.



      Undesirable:



      apples apples apples banana
      banana. cantaloupe cantaloupe


      Desirable:



      % a line break is inserted at the beginning
      % of the bananas because they won't all fit
      % on the current line.

      apples apples apples
      banana banana. cantaloupe cantaloupe


      How can we write LaTeX code which makes this happen?



      The Long Story



      Consider the following test file. It will not compile because "KEEP TOGETHER" is not yet defined. The question can be viewed as: What do we need to do the make the following file produce the desired behavior?



      documentclass{minimal}
      begin{document}

      apples apples apples apples
      apples apples apples apples
      apples apples apples apples

      begin{KEEP TOGETHER}
      Banana tincidunt ante banana.
      end{KEEP TOGETHER}

      cantaloupe cantaloupe cantaloupe
      cantaloupe cantaloupe cantaloupe
      cantaloupe cantaloupe.

      end{document}


      Suppose that STRYNG is the text block enclosed by begin{KEEP TOGETHER} and end{KEEP TOGETHER}. We wish to have the following behavior:



      SPACER = four character indent based on
      current font size and other settings.

      if (STRYNG will fit on what remains
      of the current line) {
      WILL_FIT_HERE = true;
      }
      if (STRYNG will fit on an almost empty
      blank line, SPACER at the beginning) {
      WILL_FIT_NEXT = true;
      }
      if [(not WILL_FIT_HERE) and (WILL_FIT_NEXT)] {
      print a line break
      print SPACER on the brand new empty line.
      print all of STRYNG after the indent
      print a line-break.
      else {
      print STRYNG in the output document as if STRYNG
      were not inside of a "KEEP TOGETHER" block.

      Exhibit the same behavior as if the
      lines begin{KEEP TOGETHER} and
      end{KEEP TOGETHER} were commented out.

      This probably means that STRYNG (or at least
      the beginning of string) will be printed on
      the current line.
      }


      Similar, but Different, Stack Exchange Questions



      There is a question which embodies the same sentiments as the current question, but that question was much too vague. It was not clear what was wanted. Some people interpreted the question to mean, "how can we prevent a line break in the middle of a word?" They thought it was okay to have half of the string on on the current line and the second half on the next line. There were other interpretations as well.



      The asker of prevent-line-break-in-a-span-of-text was interested in displaying URLs containing no space characters. Like me, they want a line break inserted at the very beginning of their text, instead of in the middle. However, they do not describe what behavior they want if the URL cannot all fit on one line. In the question you are reading, I do describe what behavior is desired in that instance. Also, for the other asker, maybe LaTeX was inserting line break into their urls before the right-margin was reached. Maybe they wanted characters in the URL to run all the way to the right-margin, and only then have a line break inserted then, but not any earlier than that. I am not sure what they wanted.



      Unacceptable Solutions



      Unacceptable: Always put a line break at the beginning



      always inserting a line-break looks funny if there was plenty of space on the line above to fit the text you want to keep together.



      Unacceptable: Non-breaking spaces (~)



      Writing things like Cras~mattis~tincidunt~ante is not okay for a few reasons. For one, ~ must be inserted everywhere, which is a nuisance. Another is that the string to be "kept together," might not contain any space characters, won't all fit on the current line, but could fit on a new line all by itself. We want something similar to begin{command}{BLOCK OF TEXT}end{command}.



      Unacceptable: nolinebreak



      nolinebreak works once EXACTLY where you put it and then is not in effect after that point. Inserting nolinebreak in a dozen different places would make the source code unreadable. The issue is similar to using ~.









      share







      New contributor




      IdleCustard is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.












      The Short Story



      If some text won't all fit on the current line, we want all of that text moved to the next line instead. How can we do this?



      The Medium-Length Story



      Suppose we have a string of text, "banana banana." Having the first few characters of "banana [...]" on the current line and the second half of the banana string the next line is undesirable.



      If the entire string will fit on the current line, then good, put the string on the current line. Otherwise put the string on the next line.



      Undesirable:



      apples apples apples banana
      banana. cantaloupe cantaloupe


      Desirable:



      % a line break is inserted at the beginning
      % of the bananas because they won't all fit
      % on the current line.

      apples apples apples
      banana banana. cantaloupe cantaloupe


      How can we write LaTeX code which makes this happen?



      The Long Story



      Consider the following test file. It will not compile because "KEEP TOGETHER" is not yet defined. The question can be viewed as: What do we need to do the make the following file produce the desired behavior?



      documentclass{minimal}
      begin{document}

      apples apples apples apples
      apples apples apples apples
      apples apples apples apples

      begin{KEEP TOGETHER}
      Banana tincidunt ante banana.
      end{KEEP TOGETHER}

      cantaloupe cantaloupe cantaloupe
      cantaloupe cantaloupe cantaloupe
      cantaloupe cantaloupe.

      end{document}


      Suppose that STRYNG is the text block enclosed by begin{KEEP TOGETHER} and end{KEEP TOGETHER}. We wish to have the following behavior:



      SPACER = four character indent based on
      current font size and other settings.

      if (STRYNG will fit on what remains
      of the current line) {
      WILL_FIT_HERE = true;
      }
      if (STRYNG will fit on an almost empty
      blank line, SPACER at the beginning) {
      WILL_FIT_NEXT = true;
      }
      if [(not WILL_FIT_HERE) and (WILL_FIT_NEXT)] {
      print a line break
      print SPACER on the brand new empty line.
      print all of STRYNG after the indent
      print a line-break.
      else {
      print STRYNG in the output document as if STRYNG
      were not inside of a "KEEP TOGETHER" block.

      Exhibit the same behavior as if the
      lines begin{KEEP TOGETHER} and
      end{KEEP TOGETHER} were commented out.

      This probably means that STRYNG (or at least
      the beginning of string) will be printed on
      the current line.
      }


      Similar, but Different, Stack Exchange Questions



      There is a question which embodies the same sentiments as the current question, but that question was much too vague. It was not clear what was wanted. Some people interpreted the question to mean, "how can we prevent a line break in the middle of a word?" They thought it was okay to have half of the string on on the current line and the second half on the next line. There were other interpretations as well.



      The asker of prevent-line-break-in-a-span-of-text was interested in displaying URLs containing no space characters. Like me, they want a line break inserted at the very beginning of their text, instead of in the middle. However, they do not describe what behavior they want if the URL cannot all fit on one line. In the question you are reading, I do describe what behavior is desired in that instance. Also, for the other asker, maybe LaTeX was inserting line break into their urls before the right-margin was reached. Maybe they wanted characters in the URL to run all the way to the right-margin, and only then have a line break inserted then, but not any earlier than that. I am not sure what they wanted.



      Unacceptable Solutions



      Unacceptable: Always put a line break at the beginning



      always inserting a line-break looks funny if there was plenty of space on the line above to fit the text you want to keep together.



      Unacceptable: Non-breaking spaces (~)



      Writing things like Cras~mattis~tincidunt~ante is not okay for a few reasons. For one, ~ must be inserted everywhere, which is a nuisance. Another is that the string to be "kept together," might not contain any space characters, won't all fit on the current line, but could fit on a new line all by itself. We want something similar to begin{command}{BLOCK OF TEXT}end{command}.



      Unacceptable: nolinebreak



      nolinebreak works once EXACTLY where you put it and then is not in effect after that point. Inserting nolinebreak in a dozen different places would make the source code unreadable. The issue is similar to using ~.







      line-breaking





      share







      New contributor




      IdleCustard is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.










      share







      New contributor




      IdleCustard is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.








      share



      share






      New contributor




      IdleCustard is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.









      asked 3 mins ago









      IdleCustardIdleCustard

      1476




      1476




      New contributor




      IdleCustard is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.





      New contributor





      IdleCustard is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.






      IdleCustard is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.






















          0






          active

          oldest

          votes











          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',
          autoActivateHeartbeat: false,
          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
          });


          }
          });






          IdleCustard is a new contributor. Be nice, and check out our Code of Conduct.










          draft saved

          draft discarded


















          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2ftex.stackexchange.com%2fquestions%2f469904%2fhow-can-we-conditionally-insert-a-line-break-at-the-beginning-of-some-text%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown

























          0






          active

          oldest

          votes








          0






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes








          IdleCustard is a new contributor. Be nice, and check out our Code of Conduct.










          draft saved

          draft discarded


















          IdleCustard is a new contributor. Be nice, and check out our Code of Conduct.













          IdleCustard is a new contributor. Be nice, and check out our Code of Conduct.












          IdleCustard is a new contributor. Be nice, and check out our Code of Conduct.
















          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.




          draft saved


          draft discarded














          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2ftex.stackexchange.com%2fquestions%2f469904%2fhow-can-we-conditionally-insert-a-line-break-at-the-beginning-of-some-text%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

          Entries order in /etc/network/interfaces

          新発田市

          Grub takes very long (several minutes) to open Menu (in Multi-Boot-System)