TransWikia.com

Underscored foreach variable inside caption not working

TeX - LaTeX Asked by james watt on June 3, 2021

I have the following latex code

documentclass[a4paper,11pt]{book}
usepackage{pgffor}
usepackage{graphicx}
usepackage[margin=1.25in]{geometry}
usepackage{physics}
usepackage{amsfonts} 
usepackage{amsmath} 
usepackage[nottoc]{tocbibind}
usepackage[nodisplayskipstretch]{setspace}
usepackage{float} 
usepackage{caption}
usepackage{subcaption}
usepackage{bm}
usepackage[T1]{fontenc}

begin{document}

foreach datasetnLayers in {donut_1d/15, donut_2d/17, squares_2d/8, spiral_2d/7}
{
    begin{figure}[H]
        centering
        foreach etas in {0, 1e-5, 1e-4, 1e-3, 1e-2, 1e-1}
        {
            begin{subfigure}{0.32225linewidth}
                centering
                includegraphics[width=textwidth]{../TesiMagistrale/PredictionsW2NormReg/pred_dataset_ResNet_nLayers_etas}
            end{subfigure}%
            vspace{0.1cm}%
        }
        caption{dataset}
    end{figure}
}

end{document}

but in the caption I get the interpreted variable for dataset so I get the subscript numbers. I want to have the native variable text with the underscore in the caption. Moreover I want it with texttt. I looked at some other questions with underscore and caption but they seem not to help me. They used protect and detokenize but I tried and nothing helped me. I added also the packages I’m using. This is what I get

enter image description here

Any suggestion?

One Answer

Try detokenizeexpandafter:

documentclass[a4paper,11pt]{book}
usepackage{pgffor}
usepackage[demo]{graphicx}
usepackage[margin=1.25in]{geometry}
usepackage{physics}
usepackage{amsfonts} 
usepackage{amsmath} 
usepackage[nottoc]{tocbibind}
usepackage[nodisplayskipstretch]{setspace}
usepackage{float} 
usepackage{caption}
usepackage{subcaption}
usepackage{bm}
usepackage[T1]{fontenc}

begin{document}

foreach datasetnLayers in {donut_1d/15, donut_2d/17, squares_2d/8, spiral_2d/7}
{
    begin{figure}[H]
        centering
        foreach etas in {0, 1e-5, 1e-4, 1e-3, 1e-2, 1e-1}
        {
            begin{subfigure}{0.32225linewidth}
                centering
                includegraphics[width=textwidth]{../TesiMagistrale/PredictionsW2NormReg/pred_dataset_ResNet_nLayers_etas}
            end{subfigure}%
            vspace{0.1cm}%
        }
        caption{texttt{detokenizeexpandafter{dataset}}}
    end{figure}
}
end{document}

Which results in:

enter image description here

Correct answer by DG' on June 3, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP