8

In relation with this example: New command without brackets inside new environment, I have to pass the current font size to the environment, but I prefer it takes it by itself?

To make it clear, I would like to store the selectfont parameters and reuse them later.

There is an obvious bracket solution, but the OP of the related question doesn't want to use brackets.

\documentclass{article}
\pagestyle{empty}

\begin{document}

\begin{itemize}

\item Bla

\item{\small Small is small} 

\item Bla is not small, but the OP of the related question doesn't want to use brackets.

\end{itemize}

\end{document}

I then try to make a switch system inside the \item macro and for this I have to know what are the fontsize parameters, to store and restore them.

\documentclass{article}
\pagestyle{empty}
\usepackage{paralist,xparse}

\newcommand{\Com}[1][\hspace{1cm}]{\item[#1\aftergroup\footnotesize]}

\NewDocumentEnvironment{Itemize}
    {O{}    % item caracter ex $\bullet$
    D<>{\normalsize}}{%
    \begin{list}{#2#1\aftergroup#2}{%
        \setlength{\labelwidth}{0em} % adapt as you want
        \setlength{\leftmargin}{0pt}
        \setlength{\itemindent}{5pt}%
    }}{%
    \end{list}}


\begin{document}

\large

\begin{Itemize}[$\bullet$]<\large>
\item One
\item Two
\Com This is my comment
\item Three
\Com This is my comment
\end{Itemize}

Back to current font size !

\end{document}
Marvin
  • 147
Tarass
  • 16,912
  • 3
    Have you seen https://tex.stackexchange.com/questions/24599/what-point-pt-font-size-are-large-etc – Torbjørn T. Mar 26 '18 at 20:44
  • @Christian Hupfer did you see my edit ? – Tarass Mar 26 '18 at 20:53
  • @Tarass: You edited your question after closing, now it's going to the reopen queue... –  Mar 26 '18 at 20:54
  • It is written that I have to edit and explain, so did I ;-) – Tarass Mar 26 '18 at 20:56
  • 1
    Your edit should aim to explain why this isn't a duplicate of the indicated question (if you think it isn't). But if you just need to reset to the original font size, that's a different matter (that you really should have explained originally). In that case, I believe that changing the font size within the new environment should mean that the font will automatically change back afterward. – Teepeemm Mar 26 '18 at 21:00
  • @Teepeemm The font size switch several times inside the environment, I have to swicth between small or footnotesize and the current size as it explain in the related question. – Tarass Mar 26 '18 at 21:05
  • 1
    \makeatletter\newcommand{\savefont}{\xdef\oldfontsize{\f@size}\xdef\oldblskip{\baselineskip}} \newcommand{\backtoppevfont}{\fontsize{\oldfontsize}{\oldblskip}} \makeatother and use them at the begining and end of the environment – koleygr Mar 26 '18 at 21:13
  • I really appriciate your kindness. It works perfectly with a final \selectfont. Consider to write an answer, I never saw your answer elsewhere. Thank you. – Tarass Mar 26 '18 at 21:20
  • Here is the duplicate of the answer if reopened to be re-closed :P https://tex.stackexchange.com/questions/322256/what-is-the-second-value-in-fontsize-for. Welcome! – koleygr Mar 26 '18 at 21:23
  • The question needs to be reopened before it can be answered. Also, the second parameter of \fontsize is stored in f@baselineskip (you probably won't need this since you already have an answer, but it is good to know). – Circumscribe Mar 26 '18 at 21:23
  • @koleygr What you wrote is not on the page you linked, I do not see it. – Tarass Mar 26 '18 at 21:27
  • @Circumscribe It could be usefull for someone else. – Tarass Mar 26 '18 at 21:28
  • At the moment I answered the edited question had the phrase: "More exactly : how to find the second paramater of \fontsize{...}{...}selectfont ?parameters and reuse them latter.". So, this is why I made the above comment... Of course This is not the full answer given there... but the most of the people will find this instead if looking for that... Any way, already voted to reopen. – koleygr Mar 26 '18 at 21:33
  • 1
    I rephrase the title of my question in a more explicit way. I didn't know where it will lead at the first time, the precision came with the comments. – Tarass Mar 26 '18 at 21:40
  • 1
    Why do you even need this? The font sizes are changing back to the previous size, don't they? – Skillmon Mar 26 '18 at 21:44
  • may be deleting the word "parameters" from the title would make it more helpful (explanation: Even people that are not aware of \fontsize command would find it earier. May be spliting the word fontsize would be nice too for the same reason) – koleygr Mar 26 '18 at 21:45
  • @Skillmon Look, please, to my answer https://tex.stackexchange.com/questions/423186/new-command-without-brackets-inside-new-environment/423216?noredirect=1#comment1058949_423216, you will see that I have to switch the font size. The problem is not at the end of the enviroment but in the switches. If you have a better idea to to answer this question, I am interested to learn. – Tarass Mar 26 '18 at 21:47
  • @koleygr I am not very good at english and not very aware of howto ask a question with the right way. Please, fill free (you or someone else) to rephrase what is needed to make it usefull. On my side I have my answer and I can even delete the question, but i still think it could be usefull. – Tarass Mar 26 '18 at 21:50
  • @Tarass, I am not good in English too. Just suggested because I know how people are googling or are supposed to google... I used many times this for some reasons and also think that it is useful (even as a closed question). By the way I found this: https://tex.stackexchange.com/questions/308872/saving-and-restoring-font-settings And may have to answer there as an alternative answer... – koleygr Mar 26 '18 at 21:55
  • @koleygr I made an edit following your suggestion and taking account of the brackets solution you are pointing. – Tarass Mar 26 '18 at 22:17
  • You could as well do some nasty argument grabbing and only assign the size for \Com locally. But I guess that wouldn't be very robust (though I've wrote something which works with your MWE) :) – Skillmon Mar 26 '18 at 22:32

2 Answers2

6
\documentclass{article}
\usepackage{lipsum}
\usepackage{pgf}
\usepackage{pgffor}


%This is my suggestion (with @Circumscribe's comment in account):

\makeatletter
\newcommand{\savefont}{\xdef\oldfontsize{\f@size}\xdef\oldblskip{\f@baselineskip}}
\newcommand{\backtoppevfont}{\fontsize{\oldfontsize}{\oldblskip}\selectfont}
\makeatother


%Just added to demonstrate "unknown" font
\def\randomSize{%
\pgfmathsetmacro{\a}{random(1,4)}%
\foreach \fnt[count=\i from 1] in {\tiny,\footnotesize,\large,\Large}{%
\ifnum \a=\i\global\let\fs\fnt\breakforeach
\fi}\expandafter\string\fs\fs%
}
\begin{document}


Here is a text... and a random change of font to:\randomSize'' Test! RFont \savefont

That turned into \small\verb|\small| here and restored to \backtoppevfont RFont1!

\end{document}

Output:

enter image description here

koleygr
  • 20,105
3

Save the current font and add some code to \item so the font is restored after issuing the needed \par.

\documentclass{article}
\usepackage{etoolbox}

\usepackage[nopar]{lipsum} % mock text

\newif\ifcomment
\newcommand{\Com}{\par\footnotesize\itshape\commenttrue}

\newenvironment{Itemize}
 {%
  \edef\Itemizecurrent{\the\font}%
  \itemize
  \preto{\item}{\ifcomment\par\Itemizecurrent\fi}%
 }{%
   \enditemize
 }


\begin{document}

\begin{Itemize}
\item One
\item \lipsum[4]
\Com \lipsum[5]
\item Three
\Com This is my comment
\end{Itemize}

Back to current font size!

\end{document}

enter image description here

egreg
  • 1,121,712