2

I want to truncate (cut/shorten) a string that contains newline (\\ or \newline) in such a way that only the first line remains.

My problem is, that I use the todonotes package and have command wrapped around the normal \todo. Here is a minimal working example: (my actual command is more complex)

\newcommand{\TODO}[2]{\todo[#1,inline, caption={#2}]{#2}} 

The problem is, that I might call that function with something that contains a newline. This will fail while setting the caption. Also, it is a bad idea to have a multi-line caption.

 \TODO{foo \\ bar} 

Therefore I want to truncate the string first. I tried the xstring package, but I can not find anything on how to do it for newline (\\ or \newline). I could use xstring to cut the string after x characters, but I do not know how soon the \\ will happen.

2
  • you haven't provided a test file but I assume caption={\zz#2\\\relax} works were \zz is defined by \def\zz#1\\#2\relax{#1} Commented Jan 28, 2016 at 14:51
  • \let\newline=\relax ? Not sure if that would work on \\ though. Commented Jan 28, 2016 at 15:12

1 Answer 1

2

Assuming that \TODO has an optional argument for specifying possible other options to \todo and a mandatory argument, which you want to truncate only in the list of todos, here's a way:

\documentclass{article} \usepackage{xparse} \usepackage{todonotes} \ExplSyntaxOn \NewDocumentCommand{\TODO}{ O{} m } { \seq_set_split:Nnn \l_tmpa_seq { \\ } { #2 } \masgo_todo:nfn { #1 } { \seq_item:Nn \l_tmpa_seq { 1 } } { #2 } } \cs_new_protected:Nn \masgo_todo:nnn { \todo[inline, caption={#2}, #1]{#3} } \cs_generate_variant:Nn \masgo_todo:nnn { nf } \ExplSyntaxOff \begin{document} \listoftodos \section{Test} First\TODO{x} Second\TODO{x \\ y} Third\TODO{x \\ y \\ z} \end{document} 

The mandatory argument is split at \\ and then the first item is used for the caption, while the whole argument is used for the note.

enter image description here

6
  • First, let me thank you and second: Why does this work? What magic happens between \ExplSyntaxOn and \ExplSyntaxOff? (And why doesn't Google find anything useful about the commands you use there?) Commented Jan 29, 2016 at 9:31
  • @masgo Essentially, it is “split off the mandatory argument whatever comes from the first \\ and use it for the caption. Doing it with a sequence is not the only way, but, as you see, it just requires very short code and it allows for further modifications: it would be easy to change the \\ into spaces for getting the full caption without line breaks. Commented Jan 29, 2016 at 9:36
  • Can you point me to some resource that explains the commands you use (\seq_set_split:Nnn \l_tmpa_seq \seq_item:Nn \cs_new_protected:Nn \cs_generate_variant:Nn). Google just gives me more stackoverflow answers that use them without explaination. The only thing I could find is \ExplSyntaxOn and the xparse documentation that explains NewDocumentCommand Commented Jan 29, 2016 at 9:49
  • @masgo On your TeX system you should be able to do texdoc interface3 and texdoc xparse (or go to texdoc.net) Commented Jan 29, 2016 at 9:50
  • I found out, that the solution does not work if I have a new paragraph in my todo note (two consecutive new lines leading to a \par .. or simply a \par) .. I tried to change \NewDocumentCommand{\TODO}{ O{} m } to \NewDocumentCommand{\TODO}{ O{} +m } according to the xparse documentation this should make the command also parse paragraphs, but somehow it does not. Commented Feb 17, 2016 at 9:15

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.