# LaTex how to auto break line within text subscript environment

I'm using

 \newcommand{\textss}[1]{\textsubscript{\,\underline{#1}}}


to make text comment like this:

Here are some text\textss{and I made some comments here,
but the text maybe too long to feed a line...}


Here, the long subscript comments will not auto break new line and reach out of the PDF margin.

How to make it automatically new lined? Thanks.

Answer that emerged from the chat: not a real superscript but simple.

\documentclass{article}
\usepackage{lipsum}
\usepackage[normalem]{ulem}
\newcommand{\textss}[1]{\,\scriptsize\uline{#1}}
\begin{document}
\lipsum[1-5]

more text and more text and\textss{more text and more text and more text and more text
and more text and more text and more text and
more text and more text and more text and more text
and more text}
\end{document}


This works over a page break. The following, older proposals do not (but are subscripts).

Here is a possible way. It uses (a variant of) this answer to measure the remainder of the line. You need to compile twice.

\documentclass{article}
\usepackage{soul}
\usepackage{tikzpagenodes}
\makeatletter
\newcommand{\measureremainder}[1]{%based on https://tex.stackexchange.com/a/17808
\begin{tikzpicture}[overlay,remember picture]
\path (0,0) coordinate (aux);
\pgfpointdiff{\pgfpointanchor{aux}{center}}{\pgfpointanchor{current page text area}{east}}
\xdef#1{\the\pgf@x}
\end{tikzpicture}%
}
\makeatother
\newcommand{\textss}[1]{\textsubscript{\,\measureremainder{\myrem}%
\begin{minipage}[t]{\the\dimexpr\myrem-0.5pt}
\scriptsize\ul{#1}
\end{minipage}}}
\begin{document}
Marmots are very good at hibernating\textss{and I made some comments here, but the text maybe too long to feed a line}
and cats do not only write catcode.
\end{document}


\documentclass{article}
\usepackage[normalem]{ulem}
\usepackage{tikzpagenodes}
\makeatletter
\newcommand{\measureremainder}[1]{%based on https://tex.stackexchange.com/a/17808
\begin{tikzpicture}[overlay,remember picture]
\path (0,0) coordinate (aux);
\pgfpointdiff{\pgfpointanchor{aux}{center}}{\pgfpointanchor{current page text area}{east}}
\xdef#1{\the\pgf@x}
\end{tikzpicture}%
}
\newcommand{\measurevertdist}[1]{%
\begin{tikzpicture}[overlay,remember picture]
\path (0,0) coordinate (aux1);
\pgfpointdiff{\pgfpointanchor{aux1}{center}}{\pgfpointanchor{aux}{center}}
\xdef#1{\the\pgf@y}
\end{tikzpicture}%
}

\makeatother
\newcommand{\textss}[1]{\textsubscript{\,\measureremainder{\myrem}%
\hspace*{\the\dimexpr-1\linewidth+\myrem+0.5pt}\begin{minipage}[t]{\the\dimexpr\textwidth-1pt}
\hspace*{\the\dimexpr\linewidth-\myrem}\scriptsize\uline{#1}%
\measurevertdist{\mydist}%
\measureremainder{\myrem}%
\end{minipage}}%
\ifdim\mydist>5pt\relax
\\[\the\dimexpr\mydist-\baselineskip]%
\hspace*{\the\dimexpr\linewidth-\myrem-0.5ex}
\else
\hspace*{-\myrem}%
\fi}
\begin{document}
Marmots are very good at hibernating\textss{and I made some comments here, but the text maybe too long to feed a line}
and cats do not only write catcode.

Ducks are very good swimmers\textss{and I made some comments here, but the text maybe too long to feed a line}
and cats do not only write catcode.

Ducks\textss{ducks} pft and more text and more text and more text and more text
and more text and more text and more text.
\end{document}


• @coanor Do you get the error when running the exact code from my answer? The error message suggests that you did not add \newcommand{\measureremainder}[1]{...  sandwiched between \makeatletter and \makeatother to your document. – user194703 Feb 23 at 3:50
• It's working within a single tex, but when integrated into my working paper, I got the error:! Undefined control sequence. \measureremainder ...a}{east}} \xdef #1{\the \pgf @x} \end {tikzpicture} l.144 ...t the text maybe too long to feed a line} – coanor Feb 23 at 3:53
• @coanor Well, this answer is working. I do not know the code of your working paper, nor the way you incorporate the code from the answer into it. It should work if you add the preamble of this answer including the \makeatletter and \makeatother commands. Can you post a minimal example that yields the error (if you really get an error after copying the code as is)? – user194703 Feb 23 at 3:57
• Well, I fixed it, I missing makeatletter. -__- – coanor Feb 23 at 4:11
• Awesome answer, thanks great! And I have some suggestions: Is it possible to make the line breaking begin from the normal line header instead of current position? Another is, the soul package for underline seems not perfect, recommend using ulem's \uline: tex.stackexchange.com/questions/9550/… – coanor Feb 23 at 4:23