Manual Solution:
You could also do it manually, by inserting a \BreakableChar
at the appropriate points:
Thisisaverylongtexttosimulateerror “firstterm*[title/abstract] AND
(secondterm*\BreakableChar{}[title/abstract] OR thirdterm*[title/abstract])”
One small complication here is that I have defined \BreakableChar
with an optional parameter, and since your subsequent text begins with a [
, it necessitated the use of {}
.
Automated Solution:
You could adapt the solution from Automatic line breaking of long lines of text
too automatically insert a breakable char at the points where you want it. To use this just pass the entire string to \AddBreakableChars{}
:
Thisisaverylongtexttosimulateerror “firstterm*[title/abstract] AND
\AddBreakableChars{(secondterm*[title/abstract]} OR thirdterm*[title/abstract])”
The current definition of it will insert a \BreakableChar
following any *
or \
. This can be extended to apply to additional characters as well. I have also shown in commented code what to do for cases where there is a character where you want to break and do want to insert a hyphen.
Results:
Here is the original text followed by the text where the term is given to the automated, \AddBreakableChars{}
macro, and the one with a \BreakableChar
manually inserted at the appropriate point:

Code:
\documentclass{article}
\usepackage{showframe}
\usepackage{hyphenat}
\usepackage{xstring}
\usepackage{forloop}
\newsavebox\MyBreakChar%
\sbox\MyBreakChar{}% char to display the break after non char
\newsavebox\MySpaceBreakChar%
\sbox\MySpaceBreakChar{\hyp}% char to display the break after space
\makeatletter%
\newcommand*{\BreakableChar}[1][\MyBreakChar]{%
\leavevmode%
\prw@zbreak%
\discretionary{\usebox#1}{}{}%
\prw@zbreak%
}%
\makeatother
\newcounter{index}%
\newcommand{\AddBreakableChars}[1]{%
\StrLen{#1 }[\stringLength]%
\forloop[1]{index}{1}{\value{index}<\stringLength}{%
\StrChar{#1}{\value{index}}[\currentLetter]%
\IfStrEqCase{\currentLetter}{%
% All the characters where you don't want hypen
{*}{\currentLetter\BreakableChar[\MyBreakChar]}%
{/}{\currentLetter\BreakableChar[\MyBreakChar]}%
% All the charactes where a break should have a hypen
%{ }{\currentLetter\BreakableChar[\MySpaceBreakChar]}%
}[\currentLetter]%
}%
}%
\begin{document}
\noindent\textbf{Original Text}:
\par\noindent
Thisisaverylongtexttosimulateerror “firstterm*[title/abstract] AND
(secondterm*[title/abstract] OR thirdterm*[title/abstract])”
\medskip
\noindent\textbf{Use automated} \verb|\AddBreakableChars|:
\par\noindent
Thisisaverylongtexttosimulateerror “firstterm*[title/abstract] AND
\AddBreakableChars{(secondterm*[title/abstract]} OR thirdterm*[title/abstract])”
\medskip
\noindent\textbf{Use manual} \verb|\BreakableChar|:
\par\noindent
Thisisaverylongtexttosimulateerror “firstterm*[title/abstract] AND
(secondterm*\BreakableChar{}[title/abstract] OR thirdterm*[title/abstract])”
\end{document}