2

I have a requirement that certain words should be formatted separately in the main text. For this, I have written a new command

\newcommand{\concept}[1]{\textcolor{gray}{\texttt{#1}}}

I don't want the words that will be written inside \concept{} command to be hyphenated. They can be pushed to the next line, but not hyphenated. What are my options here?

Edit 1: Added the packages in the MWE which shows the hyphenation in \texttt. I don't need that many entries in one paragraph, just showing how it is breaking inside tt environment. Compiling with xelatex.

\documentclass[12pt,a4paper]{article}
 \usepackage{fontspec}
 \usepackage{libertine}
 \setmonofont{Latin Modern Mono Light}
\usepackage{geometry}
\geometry{a4paper, textwidth=6.in, textheight=9.25in, marginparsep=7pt, marginparwidth=.6in}
\usepackage[svgnames]{xcolor}
\renewcommand{\baselinestretch}{1.3}
\newcommand{\concept}[1]{\textcolor{gray}{\texttt{(#1)}}}

\begin{document}

Each step in the development of the \concept{bourgeoisie} was accompanied by a corresponding \concept{political} advance of that class. An \concept{oppressed} class under the sway of the feudal nobility, an armed and self-governing association in the \concept{medieval} \concept{commune}: here independent urban republic (as in Italy and Germany); there taxable “third estate” of the \concept{monarchy} (as in France); afterwards, in the period of manufacturing proper, serving either the \concept{semi-feudal} or the absolute monarchy as a counterpoise against the \concept{nobility}, and, in fact, cornerstone of the great monarchies in general, the \concept{bourgeoisie} has at last, since the establishment of Modern Industry and of the world market, conquered for itself, in the \concept{modern} representative \concept{State}, exclusive political sway. The executive of the modern state is but a committee for managing the common affairs of the whole bourgeoisie. 
\end{document}

enter image description here

  • You can always put them in an extra mbox if you want. – Johannes_B May 21 '17 at 7:55
  • 1
    have you tried putting them in a mbox? IE \newcommand{\concept}[1]{\mbox{\textcolor{gray}[\texttt{#1}}}}? – Moriambar May 21 '17 at 7:55
  • 4
    Normally, text in \texttt is not hyphenated. – egreg May 21 '17 at 7:57
  • 1
    @Moriambar your solution worked, there is a [ instead of { in your code, rest works fine, – Damitr May 21 '17 at 8:05
  • @egreg yet it was hyphenated, I have added an image to show this – Damitr May 21 '17 at 8:05
5

You should state that text in the monospaced font should not be hyphenated, which fontspec doesn't do by default:

\documentclass[12pt,a4paper]{article}
\usepackage{geometry}
\geometry{
  a4paper,
  textwidth=6.in,
  textheight=9.25in,
  marginparsep=7pt,
  marginparwidth=.6in
}
\usepackage[svgnames]{xcolor}
\usepackage{libertine}

\setmonofont{Latin Modern Mono Light}[HyphenChar=None]

\renewcommand{\baselinestretch}{1.3}
\newcommand{\concept}[1]{\textcolor{gray}{\texttt{(#1)}}}

\begin{document}

Each step in the development of the \concept{bourgeoisie} was accompanied 
by a corresponding \concept{political} advance of that class. An 
\concept{oppressed} class under the sway of the feudal nobility, an armed 
and self-governing association in the \concept{medieval} \concept{commune}: 
here independent urban republic (as in Italy and Germany); there taxable 
“third estate” of the \concept{monarchy} (as in France); afterwards, in the 
period of manufacturing proper, serving either the \concept{semi-feudal} or 
the absolute monarchy as a counterpoise against the \concept{nobility}, and, 
in fact, cornerstone of the great monarchies in general, the 
\concept{bourgeoisie} has at last, since the establishment of Modern Industry 
and of the world market, conquered for itself, in the \concept{modern}
representative \concept{State}, exclusive political sway. The executive of
the modern state is but a committee for managing the common affairs of the
whole bourgeoisie. 

\end{document}

However, this will give many overfull lines; such paragraphs should be typeset as \sloppy; use sloppypar to confine the behavior. Using microtype reduces the ugliness.

\documentclass[12pt,a4paper]{article}
\usepackage{geometry}
\geometry{
  a4paper,
  textwidth=6.in,
  textheight=9.25in,
  marginparsep=7pt,
  marginparwidth=.6in
}
\usepackage[svgnames]{xcolor}
\usepackage{microtype}
\usepackage{libertine}

\setmonofont{Latin Modern Mono Light}[HyphenChar=None]

\renewcommand{\baselinestretch}{1.3}
\newcommand{\concept}[1]{\textcolor{gray}{\texttt{(#1)}}}

\begin{document}

\begin{sloppypar}
Each step in the development of the \concept{bourgeoisie} was accompanied 
by a corresponding \concept{political} advance of that class. An 
\concept{oppressed} class under the sway of the feudal nobility, an armed 
and self-governing association in the \concept{medieval} \concept{commune}: 
here independent urban republic (as in Italy and Germany); there taxable 
“third estate” of the \concept{monarchy} (as in France); afterwards, in the 
period of manufacturing proper, serving either the \concept{semi-feudal} or 
the absolute monarchy as a counterpoise against the \concept{nobility}, and, 
in fact, cornerstone of the great monarchies in general, the 
\concept{bourgeoisie} has at last, since the establishment of Modern Industry 
and of the world market, conquered for itself, in the \concept{modern}
representative \concept{State}, exclusive political sway. The executive of
the modern state is but a committee for managing the common affairs of the
whole bourgeoisie.
\end{sloppypar}

\end{document}

enter image description here

3

If the texttt text is still hyphenated (even if it shouldn't) or whenever you have a similar task, you could try mboxing it, ie:

 \newcommand{\concept}[1]{\mbox{\textcolor{gray}{\texttt{#1}}}}

A small sample:

\documentclass{article}
\usepackage{xcolor}
\newcommand{\concept}[1]{\mbox{\textcolor{gray}{\texttt{#1}}}}
\begin{document}
A pre-test question about how to find distance from the Earth to the Sun \concept{(collaboration)}\concept{(prior knowledge)}
\end{document}

with the result

enter image description here

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.