1

I'm writing in tufte-book class in LyX and have defined a custom tcolorbox (colored-boxes in LyX) as follows:enter image description here Now, I enter the text label={truck} in "Color Box Option" and to reference, \ref={truck}, the reference comes out as ??. Even if I copy the working example from the LyX reference document on colored boxes, it doesn't work. Inserting enter image description here results in enter image description here Since it works in the reference document, it must be something with the definition of custom box 2 or the document class? Any help resolving it is greatly appreciated.

Edit: Here is an example of a document:

    #LyX 2.3 created this file. For more info see http://www.lyx.org/
\lyxformat 544
\begin_document
\begin_header
\save_transient_properties true
\origin unavailable
\textclass tufte-book
\begin_preamble
\usepackage{xcolor} % for colour
\usepackage{lipsum} % just for sample text
\usepackage{physics}
\usepackage{caption}

% add numbers to chapters, sections, subsections
\setcounter{secnumdepth}{2}
\setcounter{tocdepth}{1}
\definecolor{oucrimsonred}{rgb}{0.6, 0.0, 0.0}
\definecolor{persianred}{rgb}{0.8, 0.2, 0.2}
\definecolor{persimmon}{rgb}{0.93, 0.35, 0.0}
\titleformat{\chapter}%
  {\huge\rmfamily\itshape\color{oucrimsonred}}% format applied to label+text
  {\llap{\colorbox{oucrimsonred}{\parbox{1.5cm}{\hfill\itshape\huge\color{white}\thechapter}}}}% label
  {2pt}% horizontal separation between label and title body
  {\leavevmode\newline}% before the title body
  [{\titlerule[0.8pt]}]% after the title body

% section format
\titleformat{\section}%
  {\normalfont\LARGE\itshape\color{persianred}}% format applied to label+text
  {\vspace{2em}\llap{\colorbox{persianred}{\parbox{1.5cm}{\hfill\color{white}\thesection}}}}% label
  {1em}% horizontal separation between label and title body
  {}% before the title body
  []% after the title body

% subsection format
\titleformat{\subsection}%
  {\normalfont\large\itshape\color{persimmon}}% format applied to label+text
  {\llap{\colorbox{persimmon}{\parbox{1cm}{\hfill\color{white}\thesubsection}}}}% label
  {1em}% horizontal separation between label and title body
  {}% before the title body
  []% after the title body

\def\mathnote#1{%
  \tag*{\rlap{\hspace\marginparsep\smash{\parbox[t]{\marginparwidth}{%
  \footnotesize#1}}}}
}
\numberwithin{equation}{chapter}
\setlength{\JustifyingParindent}{0pt}
\setlength{\parskip}{1em}
\newcommand{\me}{\mathrm{e}}
\newcommand{\nth}{n^\text{th}}
\addtolength{\jot}{1em}

\usepackage{titlesec}
\titlespacing*{\section}
{0pt}{2.2em plus 1ex minus .2ex}{4.3ex plus .2ex}
\titlespacing*{\subsection}
{0pt}{5.5ex plus 1ex minus .2ex}{4.3ex plus .2ex}

\makeatletter
\g@addto@macro \normalsize {%
 \setlength\abovedisplayskip{10pt plus 2pt minus 2pt}%
 \setlength\belowdisplayskip{10pt plus 2pt minus 2pt}%
}
\makeatother
\end_preamble
\use_default_options true
\begin_modules
theorems-ams-chap-bytype
theorems-ams-extended-chap-bytype
tcolorbox
\end_modules
\maintain_unincluded_children false
\language english
\language_package default
\inputencoding auto
\fontencoding global
\font_roman "default" "default"
\font_sans "default" "default"
\font_typewriter "default" "default"
\font_math "auto" "auto"
\font_default_family default
\use_non_tex_fonts false
\font_sc false
\font_osf false
\font_sf_scale 100 100
\font_tt_scale 100 100
\use_microtype false
\use_dash_ligatures true
\graphics default
\default_output_format default
\output_sync 0
\bibtex_command default
\index_command default
\paperfontsize default
\spacing single
\use_hyperref false
\papersize default
\use_geometry true
\use_package amsmath 1
\use_package amssymb 1
\use_package cancel 1
\use_package esint 1
\use_package mathdots 1
\use_package mathtools 1
\use_package mhchem 1
\use_package stackrel 1
\use_package stmaryrd 1
\use_package undertilde 1
\cite_engine natbib
\cite_engine_type authoryear
\biblio_style plain
\use_bibtopic false
\use_indices false
\paperorientation portrait
\suppress_date false
\justification true
\use_refstyle 1
\use_minted 0
\index Index
\shortcut idx
\color #008000
\end_index
\secnumdepth 2
\tocdepth 2
\paragraph_separation indent
\paragraph_indentation default
\is_math_indent 0
\math_numbering_side default
\quotes_style english
\dynamic_quotes 0
\papercolumns 1
\papersides 1
\paperpagestyle default
\tracking_changes false
\output_changes false
\html_math_output 0
\html_css_as_file 0
\html_be_strict false
\end_header

\begin_body

\begin_layout New Color Box Type

\begin_inset Argument 1
status open

\begin_layout Plain Layout

auto counter, number within=chapter
\end_layout

\end_inset


\begin_inset Argument 2
status open

\begin_layout Plain Layout

cBoxB
\end_layout

\end_inset


\begin_inset Argument 3
status open

\begin_layout Plain Layout

1
\end_layout

\end_inset


\begin_inset Argument 4
status open

\begin_layout Plain Layout

\end_layout

\end_inset

colback=red!5!white, colframe=red!75!black, title=
\backslash
textbf{Definition~
\backslash
thetcbcounter #1},before skip=20pt plus 2pt,after skip=20pt plus 2pt
\end_layout

\begin_layout Standard
\begin_inset Flex Custom Color Box 2
status open

\begin_layout Plain Layout
\begin_inset Argument 1
status open

\begin_layout Plain Layout
Mass–energy equivalence, label={Einstein}
\end_layout

\end_inset


\begin_inset Formula 
\[
E=mc^{2}.
\]

\end_inset


\end_layout

\end_inset


\end_layout

\begin_layout Standard
To reference the box, insert the command
\end_layout

\begin_layout Standard

\series bold

\backslash
ref{name}
\end_layout

\begin_layout Standard
as \SpecialChar TeX
 code to the text where the reference should appear.
\end_layout

\begin_layout Standard
This is the reference to Example
\begin_inset space \space{}
\end_inset


\begin_inset ERT
status open

\begin_layout Plain Layout


\backslash
ref{Einstein}
\end_layout

\end_inset

.
 
\end_layout

\end_body
\end_document
3
  • 2
    can you post the LyX file? you can open it with any text editor and post its content.
    – Udi Fogiel
    Commented Jan 13, 2023 at 13:18
  • Thank you so much for your response. I've added the text of the .lyx file now. Commented Jan 13, 2023 at 18:15
  • I've forgot to mention that the module has only Number within section and by type version, if you need a different numbering let me know and I will load one tho the repo
    – Udi Fogiel
    Commented Jan 24, 2023 at 19:59

1 Answer 1

2

What I did in the following file was to define the tcolorbox with two arguments instead of one, the first for the optional definition title, and the second for the label.

I should point out that TeX ignores spaces after command names, that is why you did not have a space in \thetcbcounter #1, I assumed that you wanted a space here so i changed it to \thetcbcounter\ #1.

#LyX 2.3 created this file. For more info see http://www.lyx.org/
\lyxformat 544
\begin_document
\begin_header
\save_transient_properties true
\origin unavailable
\textclass tufte-book
\begin_preamble

\end_preamble
\use_default_options true
\begin_modules
tcolorbox
\end_modules
\maintain_unincluded_children false
\language english
\language_package default
\inputencoding auto
\fontencoding global
\font_roman "default" "default"
\font_sans "default" "default"
\font_typewriter "default" "default"
\font_math "auto" "auto"
\font_default_family default
\use_non_tex_fonts false
\font_sc false
\font_osf false
\font_sf_scale 100 100
\font_tt_scale 100 100
\use_microtype false
\use_dash_ligatures true
\graphics default
\default_output_format default
\output_sync 0
\bibtex_command default
\index_command default
\paperfontsize default
\spacing single
\use_hyperref false
\papersize default
\use_geometry true
\use_package amsmath 1
\use_package amssymb 1
\use_package cancel 1
\use_package esint 1
\use_package mathdots 1
\use_package mathtools 1
\use_package mhchem 1
\use_package stackrel 1
\use_package stmaryrd 1
\use_package undertilde 1
\cite_engine natbib
\cite_engine_type authoryear
\biblio_style plain
\use_bibtopic false
\use_indices false
\paperorientation portrait
\suppress_date false
\justification true
\use_refstyle 1
\use_minted 0
\index Index
\shortcut idx
\color #008000
\end_index
\secnumdepth 2
\tocdepth 2
\paragraph_separation indent
\paragraph_indentation default
\is_math_indent 0
\math_numbering_side default
\quotes_style english
\dynamic_quotes 0
\papercolumns 1
\papersides 1
\paperpagestyle default
\tracking_changes false
\output_changes false
\html_math_output 0
\html_css_as_file 0
\html_be_strict false
\end_header

\begin_body

\begin_layout New Color Box Type
\begin_inset Argument 1
status open

\begin_layout Plain Layout

auto counter, number within=chapter
\end_layout

\end_inset


\begin_inset Argument 2
status open

\begin_layout Plain Layout

cBoxB
\end_layout

\end_inset


\begin_inset Argument 3
status open

\begin_layout Plain Layout

2
\end_layout

\end_inset


\begin_inset Argument 4
status open

\begin_layout Plain Layout

\end_layout

\end_inset

colback=red!5!white, colframe=red!75!black, title=
\backslash
textbf{Definition~
\backslash
thetcbcounter
\backslash
 #1},before skip=20pt plus 2pt,after skip=20pt plus 2pt, label={#2}
\end_layout

\begin_layout Standard
\begin_inset Flex Custom Color Box 2
status open

\begin_layout Plain Layout
\begin_inset Argument 1
status open

\begin_layout Plain Layout
Mass–energy equivalence
\end_layout

\end_inset


\begin_inset Argument 2
status open

\begin_layout Plain Layout
Einstein
\end_layout

\end_inset


\begin_inset Formula 
\[
E=mc^{2}.
\]

\end_inset


\end_layout

\end_inset


\end_layout

\begin_layout Standard
To reference the box, insert the command
\end_layout

\begin_layout Standard

\series bold

\backslash
ref{name}
\end_layout

\begin_layout Standard
as \SpecialChar TeX
 code to the text where the reference should appear.
\end_layout

\begin_layout Standard
This is the reference to Example
\begin_inset space \space{}
\end_inset


\begin_inset ERT
status open

\begin_layout Plain Layout


\backslash
ref{Einstein}
\end_layout

\end_inset

.
 
\end_layout

\end_body
\end_document

Note that i removed your preamble as it was not needed for the demonstration and i wanted to make the code shorter.

If you want to customize you theorems in LyX, I can suggest a module I wrote for that purposes which can be found here. With this module you can get the style that you have currently in your file, but you will have the advantage of using LyX's referencing system as usual, and the theorems layouts will look the same (inside LyX's GUI) as the ordinary theorems provided by the usual theorems modules (so you could see the definition's number).

7
  • Thank you, that worked! Commented Jan 14, 2023 at 16:23
  • I tried out fancy_theorems, and it looks beautiful. However, I faced one slight problem. If, say, an example ends at a displayed equation, then there is an extra line below the equation before the box/style terminates. Which looks odd. This doesn't happen if the example ends in text. Is there a way to fix? Commented Jan 14, 2023 at 16:29
  • @kobaltishblue_312, Thank you for trying the module! the extra space is a bug caused by the qed key, currently you can solve that by typing \qedhere at the end of the displayed equation. I believe I will fix it by tomorrow, thank you for letting me know, I missed that.
    – Udi Fogiel
    Commented Jan 14, 2023 at 16:50
  • 1
    @kobaltishblue_312, I've fixed the bug and updated the repo. You only need to replace the file fancythm.sty with the new one.
    – Udi Fogiel
    Commented Jan 14, 2023 at 18:22
  • Thank you so much for your work. The module works perfectly now and much more useful than fancy boxes. Commented Jan 15, 2023 at 9:39

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .