Algorithm left margin and Comment Customizing - latex

I'm trying to save space, reduce both margins of the algorithm in 1, and add a comment after the "do" in line 1 (see the problem in red). All my tentatives failed. I only could place the comment between the "SemCompositeIndex" and the "do."
Its compilable code follows:
\documentclass{article}
\usepackage{mdframed}
\usepackage[noend,linesnumbered,ruled,vlined]{algorithm2e}
\makeatletter
%Remove right hand margin in algorithm
\patchcmd{\#algocf#start}% <cmd>
{-1.5em}% <search>
{0pt}% <replace>
{}{}% <success><failure>
\makeatother
\begin{document}
\begin{algorithm}
\SetAlgoLined
\SetKwInOut{Input}{input}
\SetKwInOut{Output}{output}
\Input{SemCompositeIndex \tcp*[f]{XXX}(a)}
\Output{CSemCompositeIndex \tcp*[f]{YYY}(b)}
\ForEach{entry $\in$ SemCompositeIndex \tcp*[f]{XXX}}{
CSemCompositeIndex.put(entry.compositeKey, compressMatchCounter(entry.matchCounter)) \tcp*[f]{ZZZ}}
\caption{Compress}
\label{alg:compress}
\end{algorithm}
\end{document}
I'm new here. Let me know if I need to put more details!
Thank you so much for your attention and participation.
Happy new year to all of us!

To place a comment after the condition of the for loop, you can use \ForEach(\tcp*[f]{XXX}){...}{....}
\documentclass{article}
\usepackage{mdframed}
\usepackage[noend,linesnumbered,ruled,vlined]{algorithm2e}
\setlength{\algomargin}{15pt} %#Werner Solution for left margin
\makeatletter
%Remove right hand margin in algorithm
\patchcmd{\#algocf#start}% <cmd>
{-1.5em}% <search>
{0pt}% <replace>
{}{}% <success><failure>
\makeatother
\begin{document}
\begin{algorithm}
\SetAlgoLined
\SetKwInOut{Input}{input}
\SetKwInOut{Output}{output}
\Input{SemCompositeIndex \tcp*[f]{XXX}(a)}
\Output{CSemCompositeIndex \tcp*[f]{YYY}(b)}
\ForEach(\tcp*[f]{XXX}){entry $\in$ SemCompositeIndex }{
CSemCompositeIndex.put(entry.compositeKey, compressMatchCounter(entry.matchCounter)) \tcp*[f]{ZZZ}}
\caption{Compress}
\label{alg:compress}
\end{algorithm}
\end{document}

This is the default layout of an algorithm when using algorithm2e:
\documentclass{article}
\usepackage[noend,linesnumbered,ruled,vlined]{algorithm2e}
%\setlength{\algomargin}{0pt}
\begin{document}
\begin{algorithm}[H]
\SetAlgoLined
\KwData{this text}
\KwResult{how to write algorithm with \LaTeX2e }
initialization\;
\While{not at end of this document}{
read current\;
\eIf{understand}{
go to next section\;
current section becomes this one\;
}{
go back to the beginning of current section\;
}
}
\caption{How to write algorithms}
\end{algorithm}
\end{document}
You can use \setlength{\algomargin}{<len>} to change the margins. For example, this is what \setlength{\algomargin}{0pt} looks like:
The default (first image above) is \leftskip + \parindent.

Related

A lot of space at the beginning of the page in Latex

There is a lot of space in the beginning which I do no like. And also to other sections (but only the start page of the section). Do you guys have any clue on how to reduce the vertical space between the header and the actual content.
\documentclass[12pt, a4paper, oneside, openany]{scrbook}
\pagestyle{headings}
%page layout
\setlength{\voffset}{0mm}
\setlength{\topmargin}{0mm}
\setlength{\headsep}{0mm}
\setlength{\oddsidemargin}{0mm}
\setlength{\evensidemargin}{0mm}
\setlength{\textheight}{210mm}
\setlength{\textwidth}{150mm}
\setlength{\marginparwidth}{5mm}
\setlength{\marginparsep}{3mm}
\setlength{\marginparpush}{7mm}
\setlength{\parindent}{5mm}
\setlength{\footskip}{0mm}
\usepackage{fontspec}
\fontspec{Times New Roman}
\usepackage{url}
\linespread{1.5}
\usepackage[acronym]{glossaries}
\parindent 10mm
\parskip 5mm
\usepackage{graphicx}
\graphicspath{{images/}}
\usepackage[a4paper]{geometry}
You can redeclare the sectioning command which you'd like to change and set whatever beforeskip you like:
% !TeX TS-program = lualatex
\documentclass[12pt, a4paper, oneside, openany]{scrbook}
\pagestyle{headings}
\RedeclareSectionCommand[beforeskip=0pt]{chapter}
%page layout
\setlength{\voffset}{0mm}
\setlength{\topmargin}{0mm}
\setlength{\headsep}{0mm}
\setlength{\oddsidemargin}{0mm}
\setlength{\evensidemargin}{0mm}
\setlength{\textheight}{210mm}
\setlength{\textwidth}{150mm}
\setlength{\marginparwidth}{5mm}
\setlength{\marginparsep}{3mm}
\setlength{\marginparpush}{7mm}
\setlength{\parindent}{5mm}
\setlength{\footskip}{0mm}
\usepackage{fontspec}
\fontspec{Times New Roman}
\usepackage{url}
\linespread{1.5}
\usepackage[acronym]{glossaries}
\parindent 10mm
\parskip 5mm
\usepackage{graphicx}
\graphicspath{{images/}}
\usepackage[a4paper]{geometry}
\begin{document}
\chapter{title}
\end{document}
Answer courtesy of https://tex.stackexchange.com/questions/62125/how-to-remove-top-margin-above-tableofcontents. Visit this excellent post to get a comprehensive explanation and alternative methods to do your stuff.
Add the following to your preamble:
\usepackage{etoolbox}% http://ctan.org/pkg/etoolbox
\makeatletter
\let\oldtableofcontents\tableofcontents
\renewcommand{\tableofcontents}{\begingroup%
\patchcmd{\#makeschapterhead}% <cmd>
{\vspace*{50\p#}}% <search>
{}% <replace>
{}{}% <success><failure>
\oldtableofcontents%
\endgroup%
}
\makeatother
The above does a local search-and-replace within #makeschapterhead (replacing \vspace*{50\p#} with nothing, before calling the regular table of contents. The redefinition (search-and-replace) is localized within the scope of the group.

In latex, the integral symbol '\int' displays as '\Delta' in the compiled pdf, why?

This happens in some (but not all) documents, where '$\int$' in an equation displays as '$\Delta$' in the compiled pdf. For those documents, I tried both Winedt and Overleaf and the same things just happen.
The problem can be replicated by using the following codes in the main.tex file:
\documentclass[a4paper,10pt]{autart}
\pagestyle{plain}
\date{\today}
\usepackage{esint}
\usepackage{amsmath}
\usepackage{amsfonts}
\usepackage{amssymb}
\setlength {\marginparwidth }{2cm}
\begin{document}
\begin{frontmatter}
\title{Generalized $t$-Distribution Noise Model\thanksref{footnoteinfo}}
\thanks[footnoteinfo]{Acknowledgement...}
\author[AuthorCategory1]{Author 1}\ead{author1#somewhere}
\author[AuthorCategory2]{Author 2}\ead{author2#somewhere}
\address[AuthorCategory1]{Address 1}
\address[AuthorCategory2]{Address 2}
\begin{abstract}
---
\end{abstract}
\end{frontmatter}
\section{This is a section}
\begin{eqnarray}
\int \nonumber
\end{eqnarray}
\end{document}
The autart.cls can be found online, for example at https://hal.archives-ouvertes.fr/file/index/docid/514508/filename/autart.cls
You will get a result like this:
And from my own exploration, the problem can be solved surprisingly if changing the title from
\title{Generalized $t$-Distribution Noise Model\thanksref{footnoteinfo}}
to
\title{Title\thanksref{footnoteinfo}}
And you will get the correct integral symbol as follows:
It seems that you can also solve the problem by removing the "esint" package, or change the document class from autart to article. I guess there is some conflicts defined in these class/packages, maybe, and result in this funny phenomenon.
Is there a way to solve the problem without changing the title, while using autart document class and esint package?
Thanks.
Ironically, the problem is in the \no#harm macro your class defines, which redefines \protect. You can work around the problem by removing this defintion like this:
\documentclass{autart}
\pagestyle{plain}
\date{\today}
\usepackage{amsmath}
\usepackage{amsfonts}
\usepackage{amssymb}
\usepackage{xpatch}
\makeatletter
\xpatchcmd{\no#harm}{\def\protect{\noexpand\protect\noexpand}}{}{\typeout{patch ok}}{\typeout{patch fail}}
\makeatother
\usepackage{esint}
\setlength {\marginparwidth }{2cm}
\begin{document}
\begin{frontmatter}
\title{Generalized $t$-Distribution Noise Model\thanksref{footnoteinfo}}
\thanks[footnoteinfo]{Acknowledgement...}
\author[AuthorCategory1]{Author 1}\ead{author1#somewhere}
\author[AuthorCategory2]{Author 2}\ead{author2#somewhere}
\address[AuthorCategory1]{Address 1}
\address[AuthorCategory2]{Address 2}
\begin{abstract}
---
\end{abstract}
\end{frontmatter}
\section{This is a section}
\begin{eqnarray}
\int \nonumber
\end{eqnarray}
\end{document}

it doesn't show the figure but just a frame and inside the file name, although the image file is uploaded in the same folder on overleaf

May the problem consist in wrong packages? The image was uploaded successfully in the folder I tried even with the function wrapfigure but the result is the same. And I tried to use the image not through the \newcommand but it cannot be read properly anyway.
\documentclass[12pt,a4paper,oneside,draft]{article}
\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}
\usepackage{imakeidx}
\makeindex[columns=3, title=Alphabetical Index, intoc]
\usepackage{graphics}
\usepackage{graphicx}
\usepackage{wrapfig}
\graphicspath{ {images/} }
\usepackage [a4paper,top=2cm,bottom=0cm,left=1cm,right=1cm]{geometry}
\textheight=5000px % Saving trees ;-)
\usepackage{url}
%%% Macros
%%% ------------------------------------------------------------
\newlength{\spacebox}
\settowidth{\spacebox}{8888888888} % Box to align text
\newcommand{\sepspace}{\vspace*{15em}} % Vertical space macro
\newcommand{\titolo}[4]{
**\begin{figure}[t]
\includegraphics[width=5cm]{logoPolimi.png}
\centering
\end{figure}**
%\vspace*{15em}
\sepspace
\centering\textbf{\huge{#1}}
\vspace{15em}
\centering\textbf{#2}
\vspace{2em}
\textbf{#3}
\vspace{2em}
\textbf{#4}}
\usepackage{caption}
%\pagenumbering{roman}
\begin{document}
\titolo{Digital Channel}{Mario Rossi}{Management Engineering}{Politecnico di Milano}
\end{document}
Thanks in advance
The figure does not appear, because you are using draft as document class option.
Unrelated to your problem, but
font commands like \huge are switches and don't take an argument. So instead of \huge{...}, you should use {\huge ...}
you don't need the graphics package if you also load graphicx
if your tex distribution is up to date, you can also skip \usepackage[utf8]{inputenc} because this is the default for some years
\documentclass[12pt,a4paper,oneside,
%draft
]{article}
%\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}
\usepackage{imakeidx}
\makeindex[columns=3, title=Alphabetical Index, intoc]
%\usepackage{graphics}
\usepackage{graphicx}
\usepackage{wrapfig}
\graphicspath{ {images/} }
\usepackage [a4paper,top=2cm,bottom=0cm,left=1cm,right=1cm]{geometry}
\textheight=5000px % Saving trees ;-)
\usepackage{url}
%%% Macros
%%% ------------------------------------------------------------
\newlength{\spacebox}
\settowidth{\spacebox}{8888888888} % Box to align text
\newcommand{\sepspace}{\vspace*{15em}} % Vertical space macro
\newcommand{\titolo}[4]{
\begin{figure}[t]
\includegraphics[width=5cm]{example-image.png}
\centering
\end{figure}
%\vspace*{15em}
\sepspace
\centering\textbf{\huge #1}
\vspace{15em}
\centering\textbf{#2}
\vspace{2em}
\textbf{#3}
\vspace{2em}
\textbf{#4}}
\usepackage{caption}
%\pagenumbering{roman}
\begin{document}
\titolo{Digital Channel}{Mario Rossi}{Management Engineering}{Politecnico di Milano}
\end{document}

How to reset chapter and section counter with \part*

Sorry for bad english
I have a problem with my Latex code (I'm not very good in coding). I want an output like this:
Part I
Chapter I
Chapter II
Part II
Chapter I
Chapter II
I also wanted to create a box around my title of the parts , this doesn't work like I wanted, so I used \part* and \addcontentsline{toc}{part}{PART I} (to write the line in the table of content).So the code is:
\fbox{\begin{minipage}{\linewidth}
\part*{\begin{center}
PART I
\end{center}}
\end{minipage}}
\addcontentsline{toc}{part}{PART I}
\vspace{0.7cm}
I saw this methode to reset the counter of the chapters:
\makeatletter
\#addtoreset{chapter}{part}
\makeatother
but this doesn't work, I think it's due to use of \part*.
Has anyone an idea? THNANKS!
Hope the example bellow can help:)
\documentclass[a4paper,11pt,]{report}
\usepackage{hyperref}
\begin{document}
\tableofcontents
\newpage
\fbox{\begin{minipage}{\linewidth}
\part*{\begin{center}
INTRODUCTION
\end{center}}
\end{minipage}}
\addcontentsline{toc}{part}{INTRODUCTION}
\vspace{0.7cm}
\chapter{Hello}
\section{my}
\newpage
\fbox{\begin{minipage}{\linewidth}
\part*{\begin{center}
CONCLUSION
\end{center}}
\end{minipage}}
\addcontentsline{toc}{part}{CONCLUSION}
\vspace{0.7cm}
\chapter{name}
\section{is}
\end{document}
As you already know the solution for \part, how about simply using this and make it look like \part*? This has the advantage that you could add the boxes around the part titles automatically.
\documentclass[a4paper,11pt,]{report}
\usepackage[newparttoc]{titlesec}
\titleformat{\part}[frame]
{\normalfont}
{}
{8pt}
{\Large\bfseries\filcenter}
\usepackage{titletoc}
\titlecontents{part}[0em]
{\vspace{2em}\large\bfseries\sffamily\relax}
{\contentslabel[\relax]{0em}}{}{\hfill\contentspage}
\usepackage{hyperref}
\makeatletter
\#addtoreset{chapter}{part}
\makeatother
\begin{document}
\tableofcontents
\part{INTRODUCTION}
\chapter{Hello}
\section{my}
\part{CONCLUSION}
\chapter{name}
\section{is}
\end{document}

How to write "C++" in LaTeX

How can I write "C++" in LaTeX so that the output looks nice. For example C$++$ doesn't look good: the plus signs are too big and there is too much space.
The standard solution for cases like this is to use verbatim:
\verb!C++!
I've been using the code below to typset a nice looking C++ in my Master-Thesis. The code has been copied verbatim from a german forum. You should be able to just copy-paste all the code in a new .tex-document and pick the relevant stuff for you...
\documentclass{article}
\usepackage{relsize}
\usepackage{lipsum}
%c from texinfo.tex
\def\ifmonospace{\ifdim\fontdimen3\font=0pt }
%c C plus plus
\def\C++{%
\ifmonospace%
C++%
\else%
C\kern-.1667em\raise.30ex\hbox{\smaller{++}}%
\fi%
\spacefactor1000 }
%c C sharp
\def\Csharp{%
\ifmonospace%
C\#%
\else%
C\kern-.1667em\raise.30ex\hbox{\smaller{\#}}%
\fi%
\spacefactor1000 }
\begin{document}
\begin{center}
{\Huge C++ $\rightarrow$ \C++ \& \Huge C\# $\rightarrow$ \Csharp}\\
\bigskip
\ttfamily
{\Huge C++ $\rightarrow$ \C++ \& \Huge C\# $\rightarrow$ \Csharp}\\
\bigskip
\sffamily
{\Huge C++ $\rightarrow$ \C++ \& \Huge C\# $\rightarrow$ \Csharp}
\end{center}
\section{\C++}
\lipsum[1]
\subsection{\Csharp}
\lipsum[1]
\end{document}
You could try and use a typewriter font.
\texttt{C++}
I've found that the following gives good results:
\def\Cplusplus{C\raisebox{0.5ex}{\tiny\textbf{++}}}
This is what I used loooong time ago:
\newcommand*{\Cpp}{C\ensuremath{++}\xspace}
to be used like \Cpp (needs xspace package). But as you said, it is not really beautiful.
This answer, for the same question on the tex site, gives what I find to be a good looking way to this.
%C++
\newcommand\Cpp{C\nolinebreak[4]\hspace{-.05em}\raisebox{.4ex}{\relsize{-3}{\textbf{++}}}}
%C#
\newcommand\Csh{C\nolinebreak[4]\hspace{-.05em}\raisebox{.4ex}{\relsize{-3}{\textbf{\#}}}
There is a \cpluspluslogo command "with the ‘+’ signs properly positioned" in the package texlogos.
An other discussion on TeX - LaTeX Stack Exchange: Prettiest way to typeset “C++” (cplusplus)?.

Resources