4

I am having a problem with some shifted text.

Please consider this MWE:

\documentclass{article}

\usepackage[english]{babel}
\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}

\usepackage{varwidth}
\usepackage{enumerate}
\usepackage{lipsum}
\def\maxwidth{18cm}
\def\maxheight{20cm}

%%%%%%%%%%%%%%%%%%%%

% From https://tex.stackexchange.com/a/529773/152550

\newif\ifStart
\Startfalse
\newif\ifImage
\Imagefalse
\newif\ifCalloutRight
\CalloutRightfalse

\usepackage[skins,breakable]{tcolorbox}
\newsavebox\OurNiceSandBox
\tcbset{start/.code={\Starttrue},callout right/.code={\CalloutRighttrue},
shrink/.code={\tcbset{whatsapp cont/.style={hbox}}},
whatsapp cont/.style={width=\maxwidth},
whatsapp/.style={empty,breakable,
left=2mm,right=2mm,top=2mm,bottom=2mm,fontupper=\sffamily,
whatsapp cont,after skip=1ex},
whatsap skin/.style={% code for unbroken boxes:
frame code={%
\ifStart
 \ifCalloutRight
    \path[fill=#1]([xshift=-3mm]frame.north east) -- ++ (9mm,0)
    -- ([yshift=-6mm]frame.north east);
 \else
    \path[fill=#1]([xshift=3mm]frame.north west) -- ++ (-9mm,0)
    -- ([yshift=-6mm]frame.north west);
 \fi
\fi},
interior code={
\path[fill=#1,rounded corners=3mm]
 (frame.south west) rectangle (frame.north east);
},
% code for the first part of a break sequence:
skin first is subskin of={emptyfirst}{%
frame code={%
\ifStart
 \ifCalloutRight
    \path[fill=#1]([xshift=-3mm]frame.north east) -- ++ (9mm,0)
    -- ([yshift=-6mm]frame.north east);
 \else
    \path[fill=#1]([xshift=3mm]frame.north west) -- ++ (-9mm,0)
    -- ([yshift=-6mm]frame.north west);
 \fi
\fi},
interior code={
\path[fill=#1]
 (frame.south west) [rounded corners=3mm] |-
 (frame.north)  -|  (frame.east) [rounded corners=0mm] |- cycle;
},
},
% code for the middle part of a break sequence:
skin middle is subskin of={emptymiddle}{%
frame code={%
},
interior code={
\path[fill=#1]
 (frame.south west) rectangle (frame.north east);
},
},
% code for the last part of a break sequence:
skin last is subskin of={emptylast}{%
frame code={%
},
interior code={
\path[fill=#1]
 (frame.north west) [rounded corners=3mm] |-
 (frame.south)  -|  (frame.east) [rounded corners=0mm] |- cycle;
},
}}}

\newtcolorbox{Mebox}[1][]{#1,whatsapp,flush left,whatsap skin=white}
\newcommand{\Me}[2][]{\begin{lrbox}{\OurNiceSandBox}
#2
\end{lrbox}%
\begin{Mebox}[#1,shrink,varwidth upper]
#2
\end{Mebox}}

%%%%%%%%%%%%%%%%%%%%

\usepackage{listings}

\newsavebox{\fullcode}
\newcommand*{\mycode}[1]{\begin{lrbox}{#1}\begin{lstlisting}}

\newenvironment{renumerate}{
  \begin{varwidth}{\textwidth}
    \begin{enumerate}}
    {\end{enumerate}
  \end{varwidth}
}

\begin{document}

\pagecolor{red}

\mycode{\fullcode}
Example
More examples
\end{lstlisting}%
\end{lrbox}

\Me{
Example

\usebox{\fullcode}

\begin{renumerate}
    \item aa
\end{renumerate}
}

\end{document}

MWE output

Everything is fine.

However, when I want to add

\usepackage[hidelinks]{hyperref}

to the preamble, the text inside the box is shifted to the right:

The problem

I found that this has 2 fixes:

  1. Commenting \usepackage[hidelinks]{hyperref} will restore the correct alignment.
  2. Commenting

    \begin{renumerate}
        \item aa
    \end{renumerate}
    

    (and, if needed, uncommenting \usepackage[hidelinks]{hyperref}) will restore the correct alignment as well.

So my questions are:

  1. Why this is happening?
  2. How can we use \usepackage[hidelinks]{hyperref} and renumerate at the same time without shifted text?

For 1. I found Text area shifted right slightly using geometry package which proposes using \noindent, but I don't know how to use it for my code. Consider that in the full document I have a lot of \Me boxes with lstlisting and lists inside them.

EDIT As noted in comments, I could solve the problem by adding \begin{NoHyper} environment to the code. Try it out! Here is the definition from the hyperref package:

\def\NoHyper{%
  \def\hyper@link@[##1]##2##3##4{##4\Hy@xspace@end}%
  \def\hyper@@anchor##1##2{##2\Hy@xspace@end}%
  \global\let\hyper@livelink\hyper@link
  \gdef\hyper@link##1##2##3{##3\Hy@xspace@end}%
  \let\hyper@anchor\ltx@gobble
  \let\hyper@anchorstart\ltx@gobble
  \def\hyper@anchorend{\Hy@xspace@end}%
  \let\hyper@linkstart\ltx@gobbletwo
  \def\hyper@linkend{\Hy@xspace@end}%
  \def\hyper@linkurl##1##2{##1\Hy@xspace@end}%
  \def\hyper@linkfile##1##2##3{##1\Hy@xspace@end}%
  \let\Hy@backout\@gobble
}

The problem with this is that links does not work anymore (although they are still clickable, they don't go to anywhere). What lines of \def\NoHyper should we remove?

manooooh
  • 3,203
  • 2
  • 21
  • 46
  • 2
    I guess the problem is structurally similar to https://tex.stackexchange.com/q/118937/35864 or https://tex.stackexchange.com/q/423014/35864, but I'm not sure if the workarounds suggested there works here for you. – moewe Mar 09 '20 at 07:20
  • @moewe thanks for the suggestions! I tried with \begin{NoHyper}<code>\end{NoHyper} and it worked. Do you want to post an answer? – manooooh Mar 09 '20 at 10:52
  • Mhh, I don't really know what is going on and whether there might be a better solution. If one of the linked answers worked for you, closing this as a duplicate instead might be a better move. – moewe Mar 09 '20 at 16:29

0 Answers0