4

In a document of mine I would like to give people the option of filling in their email adress. However, for some exceptionally long email adresses the text box can be larger than the tikzpicture node where it should fit inside of. I now get something like this:

Text]([![https://stackoverflow.com/image.jpg

However, I would prefer that the adress would be split exactly at the "@" sign like shown below. Ideally this happens only if an email adress is too wide so maybe an if statement could help here, but I have no clue as to how.

Text]([![https://stackoverflow.com/image.jpg

Here is my code:

\documentclass{report}

%Define helvetica as font
\usepackage[scaled]{helvet}
\usepackage[T1]{fontenc}
%Packages
\usepackage[table,xcdraw]{xcolor}
\usepackage{xfp,xparse}
\usepackage{color, colortbl}
\usepackage{tikz}
\usetikzlibrary{calc}
\usepackage{wrapfig}
\usetikzlibrary{matrix,shadings}
\usetikzlibrary{decorations.text}
\definecolor{Green}{HTML}{2EC1A8}
\definecolor{Grey}{HTML}{F2F2F2}


\begin{document}
\begin{titlepage}
\begin{tikzpicture}

% Static part
\node (rect) at (0,2) [draw,color=white,fill= Green,minimum width=5cm,minimum height=1cm,text =white, 
text width = 5 cm,align=left] {\textbf{Opgemaakt door}};

\node (rect) at (0,-1) [draw,color=white,fill= Grey,minimum width=5cm,minimum height=5cm, align 
=left,text width = 5 cm,text = black,text depth = 4cm] {
{{Organisatienaam}}\\[1\baselineskip]
{{Naamadviseur}}\\[1\baselineskip]
{{Telefoonnummer}}\\[1\baselineskip]
someverylongemailadress@something.com
};


\end{tikzpicture}

\end{titlepage}

\end{document}
7

You can use the url package for this to typeset e-mail addresses as \url{emailaddress}, it defines the @ as possible break point. By default urls are typeset in monospaced font, but you can set the font to the default text font using \urlstyle{same}.

Without any further modifications the email address is split after the @:

\documentclass{report}

%Define helvetica as font
\usepackage[scaled]{helvet}
\usepackage[T1]{fontenc}
%Packages
\usepackage[table,xcdraw]{xcolor}
\usepackage{xfp,xparse}
\usepackage{color, colortbl}
\usepackage{tikz}
\usetikzlibrary{calc}
\usepackage{wrapfig}
\usetikzlibrary{matrix,shadings}
\usetikzlibrary{decorations.text}
\definecolor{Green}{HTML}{2EC1A8}
\definecolor{Grey}{HTML}{F2F2F2}
\usepackage{url}
\urlstyle{same}

\begin{document}
\begin{titlepage}
\begin{tikzpicture}

% Static part
\node (rect) at (0,2) [draw,color=white,fill= Green,minimum width=5cm,minimum height=1cm,text =white, 
text width = 5 cm,align=left] {\textbf{Opgemaakt door}};

\node (rect) at (0,-1) [draw,color=white,fill= Grey,minimum width=5cm,minimum height=5cm, align 
=left,text width = 5 cm,text = black,text depth = 4cm] {
{{Organisatienaam}}\\[1\baselineskip]
{{Naamadviseur}}\\[1\baselineskip]
{{Telefoonnummer}}\\[1\baselineskip]
\url{someverylongemailadress@something.com}
};


\end{tikzpicture}

\end{titlepage}

\end{document}

enter image description here

If you want to split before the @ then you can define @ to be a special url character, that inserts \allowbreak and then prints the @ symbol (cf. page 5 of the package documentation). However, this makes @ into a macro, so you can't just print @ in the definition of @ because that leads to infinite recursion. You can however first define a box with @ in it and print that box.

Relevant snippet:

\usepackage{url}
\urlstyle{same}

\newsavebox{\printat}
\savebox{\printat}{@}

\makeatletter\g@addto@macro\UrlSpecials{\do\@{\allowbreak\usebox{\printat}}}\makeatother

\begin{document}

enter image description here

| improve this answer | |
  • 2
    In general I'd prefer \urlstyle{same} over \urlstyle{rm} – moewe Jun 2 at 11:10
  • @moewe thanks for the suggestion, I edited it in. – Marijn Jun 2 at 11:12
  • In analogy to tex.stackexchange.com/a/477964/35864 one could use \makeatletter\g@addto@macro\UrlSpecials{\do\@{\penalty\UrlBreakPenalty\mathchar@}}\makeatother` and avoid the savebox. – moewe Jun 2 at 11:17
2

You can insert \allowbreak after @:

\documentclass{report}

%Define helvetica as font
\usepackage[scaled]{helvet}
\usepackage[T1]{fontenc}
%Packages
\usepackage[table,xcdraw]{xcolor}
\usepackage{xfp,xparse}
\usepackage{color, colortbl}
\usepackage{tikz}
\usetikzlibrary{calc}
\usepackage{wrapfig}
\usetikzlibrary{matrix,shadings}
\usetikzlibrary{decorations.text}
\definecolor{Green}{HTML}{2EC1A8}
\definecolor{Grey}{HTML}{F2F2F2}


\begin{document}
\begin{titlepage}
\begin{tikzpicture}

% Static part
\node (rect) at (0,2) [draw,color=white,fill= Green,minimum width=5cm,minimum height=1cm,text =white, 
text width = 5 cm,align=left] {\textbf{Opgemaakt door}};

\node (rect) at (0,-1) [draw,color=white,fill= Grey,minimum width=5cm,minimum height=5cm, align 
=left,text width = 5 cm,text = black,text depth = 4cm] {
{{Organisatienaam}}\\[1\baselineskip]
{{Naamadviseur}}\\[1\baselineskip]
{{Telefoonnummer}}\\[1\baselineskip]
someverylongemailadress@\allowbreak something.com

small@\allowbreak something.com
};


\end{tikzpicture}

\end{titlepage}

\end{document}
| improve this answer | |
  • A thank you that is almost entirely what I need. The only thing left is that I would prefer not to have to write that every time. Do you think it possible to define a function that adds "\allowbreak" every time after the "@" sign? – t.pellegrom Jun 2 at 10:56
  • 1
    This could be a new command \newcommand\at{@\allowbreak } with usage small\at{}something.com. But as I understand you auto-generate this file, so can replace at on generating stage. – Denys Potapov Jun 2 at 11:22

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.