1

I'm struggeling with a text format issue in LateX for a while now and I don't now how to solve it easily. I got a big document which is formatted like this:

Name1: blablablablablablablablablablablablabla
blablablablablablablablablablabla \\ (enter)
Name2: blablablablablablablablablablablablabla
blablablablablablablablablablablablablablablab
blablablabla \\ (enter)

etc.

This is really unreadable, and I'm looking for some kind of global setting that can transform my text (without editing the text) in this instead:

Name1: blablablablablablablablablablablablabla
             blablablablablablablablablablabla \\ (enter)
Name2: blablablablablablablablablablablablabla
             blablablablablablablablablablablablabla
             blablablabla \\ (enter)

So what I actually like to do, is create an indent for every sentence that is too long. Is this possible to do?

19
  • 1
    if you don't have anything else than this list in your document, then you can use \everypar and \hangindent.
    – A Gold Man
    Nov 6, 2017 at 21:06
  • "without editing the text"? Does that mean zero edits to what you have shown as input? Not even delimiting the blocks that need this special treatment? Nov 6, 2017 at 21:06
  • 1
    Welcome to TeX.SX! Is each of those blocks terminated by \\ ?
    – Skillmon
    Nov 6, 2017 at 21:08
  • @AGoldMan: Well there are some other parts that should not have the same format, thus something with \{}begin and \{}end would be preferred :)
    – Erik
    Nov 6, 2017 at 21:10
  • 1
    @AGoldMan - Hmm, \def{\\}#1:{\par\hangindent=.5in \makebox[.5in][l]{#1:}} Nov 6, 2017 at 21:23

2 Answers 2

1

You can try following macros. All between \specbegin...\specend looks exactly like in your description, i.e. you need not to do any changes in your text. The indentation af all text is calculated by the longest Name.

\newbox\specbox
\newdimen\maxname
\def\specbegin{\begingroup
   \setbox\specbox=\vbox{}%
   \maxname=0pt
   \def\p ##1{\ifx\specend##1\specend\else \expandafter\pp\expandafter##1\fi}
   \def\pp ##1: ##2\\{%
      \setbox0=\hbox{##1: }%
      \ifdim\maxname>\wd0 \else \maxname=\wd0 \fi
      \setbox\specbox=\vbox{\hbox{##2\unskip}\nointerlineskip\box0
                            \unvbox\specbox}%
      \p 
   }
   \p
}
\def\specend{
   \loop
      \setbox\specbox=\vbox{\unvbox\specbox
         \global\setbox0=\lastbox \global\setbox1=\lastbox
      }
      \unless\ifvoid0
         \noindent \hangindent=\maxname
         \hbox to\maxname{\box0\hss}\unhbox1\endgraf
      \repeat
   \endgroup
}

\specbegin
Name1: bla bla blabla blab labla blabl abla blab labla
bla blab labl ablab labl ab lab labl abla
bla blab labl ablab labl ab lab labl abla
bla blab labl ablab labl ab lab labl abla \\
Name2: blabl abla bla bla blab labl ablab lab lablabla
bl ablab labl ablabl abl abla blabl ablab labl ablablab
bla bla bla bla \\
Long Name3: blabl abla bla bla blab labl ablab lab lablabla
bl ablab labl ablabl abl abla blabl ablab labl ablablab
bl ablab labl ablabl abl abla blabl ablab labl ablablab
bla bla bla bla \\
Name4: blabl abla bla bla blab labl ablab lab lablabla
bl ablab labl blabl ablab labl ablablab
bl ablab labl ablabl abl abla blabl ablab labl ablablab
bla bla bla bla \\
\specend

The macros do two iterations over text. First pass: the longest "Name" is measured and all text is stored into \specbox. Second pass: the text is restored from \specbox and used with proper \hangindent.

0

Maybe:

\documentclass[]{article}

\makeatletter
\newenvironment{desclike}
{%
  \def\\##1{%
    \ifx##1\end%
    \else%
      \expandafter\@desclike%
    \fi##1}
  \def\@desclike##1:{%
    \if\relax\detokenize{##1}\relax%
    \else%
      \item[##1:]%
    \fi}%
  \begin{description}%
  \\%
}{%
  \end{description}
}
\makeatother

\begin{document}
\begin{desclike}
Name1: blablablablablablablablablablablablabla
blablablablablablablablablablabla \\ 
Name2: blablablablablablablablablablablablabla
blablablablablablablablablablablablablablablab
blablablabla \\
\end{desclike}
\end{document}

Like this it changes the "Name1:" to boldface, but this could be changed by changing the description labels formatting.

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.