\documentclass[12pt]{report}
\usepackage[margin=1.5cm]{geometry}
\usepackage[utf8]{inputenc}
\usepackage[portuguese]{babel}
\title{Relatório de AED}
\author{}
\date{}
\begin{document}
\maketitle
\input{section2}
\end{document}
\begin{table}[]
\begin{tabular}{|l|l|l|l|}
\hline
\multicolumn{1}{|c|}{\textbf{Método}} &
\multicolumn{1}{c|}{\textbf{Melhor caso}} &
\multicolumn{1}{c|}{\textbf{Pior caso}} &
\multicolumn{1}{c|}{\textbf{Caso esperado}} \\ \hline
Inserir novo utilizador &
O(1) &
O(n) &
O(1 + \textbackslash{}lambda) \\ \hline
Consulta dados de utilizador &
O(1) &
O(n) &
O(1 + \textbackslash{}lambda) \\ \hline
Inserir contacto &
\begin{tabular}[c]{@{}l@{}}O(1) + O(1) = O(1)\\ Find User + Insert Contact\end{tabular} &
\begin{tabular}[c]{@{}l@{}}O(n) + O(n) = O(n)\\ Find User + Insert Contact\end{tabular} &
\begin{tabular}[c]{@{}l@{}}O(1) + O(log(n)) = O(log(n))\\ Find User + Insert Contact\end{tabular} \\ \hline
Remover contacto &
\begin{tabular}[c]{@{}l@{}}O(1) + O(1) = O(1)\\ Find user + Remove Contact\end{tabular} &
\begin{tabular}[c]{@{}l@{}}O(n) + O(n) = O(n)\\ Find User + Remove Contact\end{tabular} &
\begin{tabular}[c]{@{}l@{}}O(1) + O(log(n)) = O(log(n))\\ Find User + Remove Contact\end{tabular} \\ \hline
Listagem de contactos &
\begin{tabular}[c]{@{}l@{}}O(1) + O(n) = O(n)\\ Find User + Iterate contacts\end{tabular} &
\begin{tabular}[c]{@{}l@{}}O(1) + O(n) = O(n)\\ Find User + Iterate contacts\end{tabular} &
\begin{tabular}[c]{@{}l@{}}O(1) + O(n) = O(n)\\ Find User + Iterate contacts\end{tabular} \\ \hline
Inserir novo grupo &
O(1) &
O(n) &
O(1 + \textbackslash{}lambda) \\ \hline
Consulta dados de grupo &
O(1) &
O(n) &
O(1 + \textbackslash{}lambda) \\ \hline
Remover grupo &
O(1) &
O(n) &
O(1+ \textbackslash{}lambda) \\ \hline
Inserir participante num grupo &
\begin{tabular}[c]{@{}l@{}}O(1) + O(1) = O(1)\\ Find Group + Insert Participant\end{tabular} &
\begin{tabular}[c]{@{}l@{}}O(n) + O(n) = O(n)\\ Find Group + Insert Participant\end{tabular} &
\begin{tabular}[c]{@{}l@{}}O(1) + O(log(n)) = O(log(n))\\ Find Group + Insert Participant\end{tabular} \\ \hline
Remover participante de um grupo &
\begin{tabular}[c]{@{}l@{}}O(1) + O(1) = O(1)\\ Find Group + Remove Participant\end{tabular} &
\begin{tabular}[c]{@{}l@{}}O(n) + O(n) = O(n)\\ Find Group + Remove Participant\end{tabular} &
\begin{tabular}[c]{@{}l@{}}O(1) + O(long(n)) = O(log(n))\\ Find Group + Remove Participant\end{tabular} \\ \hline
Listagem de participantes &
\begin{tabular}[c]{@{}l@{}}O(1) + O(n) = O(n)\\ Find Group + Iterate Participants\end{tabular} &
\begin{tabular}[c]{@{}l@{}}O(n) + O(n) = O(n)\\ Find Group + Iterate Participants\end{tabular} &
\begin{tabular}[c]{@{}l@{}}O(1) + O(n) = O(n)\\ Find Group + Iterate Participants\end{tabular} \\ \hline
Inserir mensagem &
\begin{tabular}[c]{@{}l@{}}O(1) + O(1) + (O(n) * O(1)) + (O(n) * O(1)) \\ = O(n)\\ Find User + Insert Message\\ + (Iterate Contacts * Insert Message) \\ + (Iterate Groups * Insert Message)\end{tabular} &
\begin{tabular}[c]{@{}l@{}}O(n) + O(n) + (O(n) * O(n)) + (O(n) * O(n)) \\ = O(n²)\\ Find User + Insert Message\\ + (Iterate Contacts * Insert Message) \\ + (Iterate Groups * Insert Message)\end{tabular} &
\begin{tabular}[c]{@{}l@{}}O(1) + O(1) + (O(n) * O(1)) + (O(n) * O(1)) \\ = O(n)\\ Find User + Insert Message\\ + (Iterate Contacts * Insert Message) \\ + (Iterate Groups * Insert Message)\end{tabular} \\ \hline
Listar mensagens de contacto &
\begin{tabular}[c]{@{}l@{}}O(1) + O(n) = O(n)\\ Find User + Iterate messages\end{tabular} &
\begin{tabular}[c]{@{}l@{}}O(n) + O(n) = O(n)\\ Find User + Iterate messages\end{tabular} &
\begin{tabular}[c]{@{}l@{}}O(1) + O(n) = O(n)\\ Find User + Iterate messages\end{tabular} \\ \hline
Listar mensagens de grupo &
\begin{tabular}[c]{@{}l@{}}O(1) + O(n) = O(n)\\ Find Group + Iterate messages\end{tabular} &
\begin{tabular}[c]{@{}l@{}}O(n) + O(n) = O(n)\\ Find Group + Iterate messages\end{tabular} &
\begin{tabular}[c]{@{}l@{}}O(1) + O(n) = O(n)\\ Find Group + Iterate messages\end{tabular} \\ \hline
\end{tabular}
\end{table}
这会超出页面右侧,我无法将其缩小以适合页面,我尝试使用 tabularx,但我不知道如何使用它。我是 Latex 的新手
答案1
您应该使用可以指定宽度并且具有允许将单元格内容分成多行的列类型的表格。例如,tabularx
其X
列类型是这样的表格:
\documentclass[12pt]{report}
\usepackage[margin=1.5cm]{geometry}
\usepackage[portuguese]{babel}
\usepackage{makecell, tabularx} % <---
\renewcommand\theadfont{\small\bfseries} % font used in column headers
\renewcommand\theadgape{} % reduce vertical gapes in column headers
\newcolumntype{L}{>{\raggedright\arraybackslash}X}
\begin{document}
\begin{table}[ht] % or htp
\small
\setlength\tabcolsep{4pt}
\begin{tabularx}{\linewidth}{|>{\hsize=0.7\hsize}L | % make the first column narrower
*{3}{>{\hsize=1.1\hsize}L | } % make other column wider
}
\Xhline{1.2pt}
\thead{Método} & \thead{Melhor caso} & \thead{Pior caso} & \thead{Caso esperado} \\
\Xhline{0.8pt}
Inserir novo utilizador
& O(1) & O(n) & O(1 + \textbackslash{}lambda) \\
\hline
Consulta dados de utilizador
& O(1) & O(n) & O(1 + \textbackslash{}lambda) \\ \hline
Inserir contacto
& O(1) + O(1) = O(1) Find User + Insert Contact
& O(n) + O(n) = O(n) Find User + Insert Contact
& O(1) + O(log(n)) = O(log(n)) Find User + Insert Contact \\ \hline
Remover contacto
& O(1) + O(1) = O(1) Find user + Remove Contact
& O(n) + O(n) = O(n) Find User + Remove Contact
& O(1) + O(log(n)) = O(log(n)) Find User + Remove Contact \\ \hline
Listagem de contactos
& O(1) + O(n) = O(n) Find User + Iterate contacts
& O(1) + O(n) = O(n) Find User + Iterate contacts
& O(1) + O(n) = O(n) Find User + Iterate contacts \\ \hline
Inserir novo grupo
& O(1) & O(n) & O(1 + \textbackslash{}lambda) \\ \hline
Consulta dados de grupo
& O(1) & O(n) & O(1 + \textbackslash{}lambda) \\ \hline
Remover grupo
& O(1) & O(n) & O(1+ \textbackslash{}lambda) \\ \hline
Inserir participante num grupo
& O(1) + O(1) = O(1) Find Group + Insert Participant
& O(n) + O(n) = O(n) Find Group + Insert Participant
& O(1) + O(log(n)) = O(log(n)) Find Group + Insert Participant \\ \hline
Remover participante de um grupo
& O(1) + O(1) = O(1) Find Group + Remove Participant
& O(n) + O(n) = O(n) Find Group + Remove Participant
& O(1) + O(log(n)) = O(log(n)) Find Group + Remove Participant \\ \hline
Listagem de participantes
& O(1) + O(n) = O(n) Find Group + Iterate Participants
& O(n) + O(n) = O(n) Find Group + Iterate Participants
& O(1) + O(n) = O(n) Find Group + Iterate Participants \\ \hline
Inserir mensagem
& O(1) + O(1) + (O(n) * O(1)) + (O(n) * O(1))
= O(n) Find User + Insert Message + (Iterate Contacts * Insert Message)
+ (Iterate Groups * Insert Message)
& O(n) + O(n) + (O(n) * O(n)) + (O(n) * O(n))
= O(n²) Find User + Insert Message + (Iterate Contacts * Insert Message)
+ (Iterate Groups * Insert Message)
& O(1) + O(1) + (O(n) * O(1)) + (O(n) * O(1))
= O(n) Find User + Insert Message
+ (Iterate Contacts * Insert Message)
+ (Iterate Groups * Insert Message) \\ \hline
Listar mensagens de contacto
& O(1) + O(n) = O(n) Find User + Iterate messages
& O(n) + O(n) = O(n) Find User + Iterate messages
& O(1) + O(n) = O(n) Find User + Iterate messages \\ \hline
Listar mensagens de grupo
& O(1) + O(n) = O(n) Find Group + Iterate messages
& O(n) + O(n) = O(n) Find Group + Iterate messages
& O(1) + O(n) = O(n) Find Group + Iterate messages \\
\Xhline{1.2pt}
\end{tabularx}
\end{table}
\end{document}
如您所见,所有插入的tabular
都被删除了,因此表格的代码明显更短更清晰。Xhline
在包中定义makecell
,它还定义了\thead
命令以及添加由单元格内容\makegapedcells
决定的垂直空间的宏。\setcellgapes
附录: 我原来的答案和@{Mico 答案的想法的结合(将右侧三列的常用文本移到第一列)。单元格仍然被垂直线和水平线牢牢地束缚着:-)
\documentclass[12pt]{report}
\usepackage[margin=1.5cm]{geometry}
\usepackage[portuguese]{babel}
\usepackage{booktabs,
makecell, tabularx}
\renewcommand\theadfont{\small\bfseries}
\renewcommand\theadgape{}
\newcolumntype{L}[1]{>{\raggedright\arraybackslash\hsize=#1\hsize}X}
\usepackage{amsmath}
\begin{document}
\begin{table}[ht]
\small
\setlength\tabcolsep{4pt}
\setcellgapes{3pt}
\makegapedcells
\begin{tabularx}{\linewidth}{|
>{\hangafter=1\hangindent=1em}L{1.6}| % that this work, you need to use `linbreak`!
*{3}{>{$}L{0.8}<{$}| }
}
\Xhline{1.2pt}
\thead{Método}
& \text{\thead{Melhor caso}}
& \text{\thead{Pior caso}}
& \text{\thead{Caso esperado}} \\
\Xhline{0.8pt}
Inserir novo utilizador
& O(1) & O(n) & O(1 + \lambda) \\ \hline
Consulta dados de utilizador\linebreak
\emph{Find User + Insert Contact}
& O(1) & O(n) & O(1 + \lambda) \\ \hline
Inserir contacto\linebreak
\emph{Find User + Insert Contact}
& O(1) + O(1) = O(1)
& O(n) + O(n) = O(n)
& O(1) + O(\log(n)) = O(\log(n)) \\ \hline
Remover contacto\linebreak
\emph{Find user + Remove Contact}
& O(1) + O(1) = O(1)
& O(n) + O(n) = O(n)
& O(1) + O(\log(n)) = O(\log(n)) \\ \hline
Listagem de contactos\linebreak
\emph{Find User + Iterate contacts}
& O(1) + O(n) = O(n)
& O(1) + O(n) = O(n)
& O(1) + O(n) = O(n) \\ \hline
Inserir novo grupo
& O(1) & O(n) & O(1 + \lambda) \\ \hline
Consulta dados de grupo
& O(1) & O(n) & O(1 + \lambda) \\ \hline
Remover grupo
& O(1) & O(n) & O(1+ \lambda) \\ \hline
Inserir participante num grupo\linebreak
\emph{Find Group + Insert Participant}
& O(1) + O(1) = O(1)
& O(n) + O(n) = O(n)
& O(1) + O(\log(n)) = O(\log(n)) \\ \hline
Remover participante de um grupo\linebreak
\emph{Find Group + Remove Participant}
& O(1) + O(1) = O(1)
& O(n) + O(n) = O(n)
& O(1) + O(\log(n)) = O(\log(n)) \\ \hline
Listagem de participantes\linebreak
\emph{Find Group + Iterate Participants}
& O(1) + O(n) = O(n)
& O(n) + O(n) = O(n)
& O(1) + O(n) = O(n) \\ \hline
Inserir mensagem\linebreak
\emph{Find User + Insert Message + (Iterate Contacts * Insert Message) + (Iterate Groups * Insert Message)}
& O(1) + O(1) + (O(n)\cdot O(1)) + (O(n)\cdot O(1)) = O(n)
& O(n) + O(n) + (O(n)\cdot O(n)) + (O(n)\cdot O(n)) = O(n²)
& O(1) + O(1) + (O(n)\cdot O(1)) + (O(n)\cdot O(1))
\\ \hline
Listar mensagens de contacto\linebreak
\emph{Find User + Iterate messages}
& O(1) + O(n) = O(n)
& O(n) + O(n) = O(n)
& O(1) + O(n) = O(n) \\ \hline
Listar mensagens de grupo\linebreak
\emph{Find Group + Iterate messages}
& O(1) + O(n) = O(n)
& O(n) + O(n) = O(n)
& O(1) + O(n) = O(n) \\
\Xhline{1.2pt}
\end{tabularx}
\end{table}
\end{document}
答案2
这是另一个tabularx
基于 的解决方案。它将第 2、3 和 4 列中重复出现的材料移到第 1 列;第一列比其他三列更宽。它还在三个数据列中自动采用数学模式。
该解决方案还致力于创造更开放的“外观”,方法是删除所有垂直线,使用更少但间距适当的水平线。希望您的读者会对您的努力做出回报,在表格上停留更长时间,并真正吸收您希望提供的信息。
\documentclass[12pt]{report}
\usepackage[T1]{fontenc}
\usepackage[margin=1.5cm]{geometry}
\usepackage[portuguese]{babel}
\usepackage{tabularx,booktabs,ragged2e}
\newcolumntype{L}[1]{>{\raggedright\arraybackslash\hsize=#1\hsize}X}
\begin{document}
\begin{table}[htbp]
\small
\setlength\tabcolsep{5pt} % default: 6pt
\begin{tabularx}{\linewidth}{@{}
>{\hangafter=1\hangindent=1em}L{1.36} % automatic hanging indentation
*{3}{>{$}L{0.88}<{$}} % automatic math mode
@{}} % Note: 1.36 + 3*0.88 = 4 = number of X-type columns
\toprule
Método & \mbox{Melhor caso} & \mbox{Pior caso} & \mbox{Caso esperado} \\
\midrule
Inserir novo utilizador nes
& O(1) & O(n) & O(1 + \lambda) \\ \addlinespace
Consulta dados de utilizador
& O(1) & O(n) & O(1 + \lambda) \\ \addlinespace
Inserir contacto.
\linebreak Find User + Insert Contact
& O(1) + O(1) = O(1)
& O(n) + O(n) = O(n)
& O(1) + O(\log(n)) \allowbreak= O(\log(n)) \\ \addlinespace
Remover contacto.
\linebreak Find User + Remove Contact
& O(1) + O(1) = O(1)
& O(n) + O(n) = O(n)
& O(1) + O(\log(n)) \allowbreak= O(\log(n)) \\ \addlinespace
Listagem de contactos.
\linebreak Find User + Iterate contacts
& O(1) + O(n) = O(n)
& O(1) + O(n) = O(n)
& O(1) + O(n) = O(n) \\ \addlinespace
Inserir novo grupo
& O(1) & O(n) & O(1 + \lambda) \\ \addlinespace
Consulta dados de grupo
& O(1) & O(n) & O(1 + \lambda) \\ \addlinespace
Remover grupo
& O(1) & O(n) & O(1 + \lambda) \\ \addlinespace
Inserir participante num grupo.
\linebreak Find Group + Insert Participant
& O(1) + O(1) = O(1)
& O(n) + O(n) = O(n)
& O(1) + O(\log(n)) \allowbreak= O(\log(n)) \\ \addlinespace
Remover participante de um grupo.
\linebreak Find Group + Remove Participant
& O(1) + O(1) = O(1)
& O(n) + O(n) = O(n)
& O(1) + O(\log(n)) \allowbreak= O(\log(n)) \\ \addlinespace
Listagem de participantes.
\linebreak Find Group + Iterate Participants
& O(1) + O(n) = O(n)
& O(n) + O(n) = O(n)
& O(1) + O(n) = O(n) \\ \addlinespace
Inserir mensagem.
\linebreak Find User + Insert Message + (Iterate Contacts * Insert Message)
+ (Iterate Groups * Insert Message)
& O(1) + O(1) + (O(n) \cdot O(1)) + (O(n) \cdot O(1)) \allowbreak= O(n)
& O(n) + O(n) + (O(n) \cdot O(n)) + (O(n) \cdot O(n)) \allowbreak= O(n^2)
& O(1) + O(1) + (O(n) \cdot O(1)) + (O(n) \cdot O(1)) \allowbreak= O(n) \\ \addlinespace
Listar mensagens de contacto.
\linebreak Find User + Iterate messages
& O(1) + O(n) = O(n)
& O(n) + O(n) = O(n)
& O(1) + O(n) = O(n) \\ \addlinespace
Listar mensagens de grupo.
\linebreak Find Group + Iterate messages
& O(1) + O(n) = O(n)
& O(n) + O(n) = O(n)
& O(1) + O(n) = O(n) \\
\bottomrule
\end{tabularx}
\end{table}
\end{document}
答案3
这是另一种选择:
\documentclass[12pt]{report}
\usepackage[T1]{fontenc}
\usepackage[margin=1.5cm]{geometry}
\usepackage[portuguese]{babel}
\usepackage{tabularx,booktabs,ragged2e}
\newcolumntype{L}{>{\raggedright\arraybackslash}X}
\begin{document}
\begin{table}[htbp]
\begin{tabularx}{\linewidth}{@{\qquad\quad}*{3}{>{$}L<{$}}@{}}
\toprule
\mbox{Melhor caso} & \mbox{Pior caso} & \mbox{Caso esperado} \\
\midrule
\multicolumn{3}{@{}l}{Inserir novo utilizador nes}\\
O(1) & O(n) & O(1 + \lambda) \\ \addlinespace
\multicolumn{3}{@{}l}{Consulta dados de utilizador}\\
O(1) & O(n) & O(1 + \lambda) \\ \addlinespace
\multicolumn{3}{@{}l}{Inserir contacto. (FU + IC)}\\
O(1) + O(1) = O(1)
& O(n) + O(n) = O(n)
& O(1) + O(\log(n)) \allowbreak= O(\log(n)) \\ \addlinespace
\multicolumn{3}{@{}l}{Remover contacto. (FU + RC)}\\
O(1) + O(1) = O(1)
& O(n) + O(n) = O(n)
& O(1) + O(\log(n)) \allowbreak= O(\log(n)) \\ \addlinespace
\multicolumn{3}{@{}l}{Listagem de contactos. (FU + ItC)}\\
O(1) + O(n) = O(n)
& O(1) + O(n) = O(n)
& O(1) + O(n) = O(n) \\ \addlinespace
\multicolumn{3}{@{}l}{Inserir novo grupo}\\
O(1) & O(n) & O(1 + \lambda) \\ \addlinespace
\multicolumn{3}{@{}l}{Consulta dados de grupo}\\
O(1) & O(n) & O(1 + \lambda) \\ \addlinespace
\multicolumn{3}{@{}l}{Remover grupo}\\
O(1) & O(n) & O(1 + \lambda) \\ \addlinespace
\multicolumn{3}{@{}l}{Inserir participante num grupo. (FG + IP)}\\
O(1) + O(1) = O(1)
& O(n) + O(n) = O(n)
& O(1) + O(\log(n)) \allowbreak= O(\log(n)) \\ \addlinespace
\multicolumn{3}{@{}l}{Remover participante de um grupo. (FG + RP)}\\
O(1) + O(1) = O(1)
& O(n) + O(n) = O(n)
& O(1) + O(\log(n)) \allowbreak= O(\log(n)) \\ \addlinespace
\multicolumn{3}{@{}l}{Listagem de participantes. (FG + ItP)}\\
O(1) + O(n) = O(n)
& O(n) + O(n) = O(n)
& O(1) + O(n) = O(n) \\ \addlinespace
\multicolumn{3}{@{}l}{Inserir mensagem. (FU + IM + (ItC * IM) + (ItG * IM))}\\
O(1) + O(1) + (O(n) \cdot O(1)) + (O(n) \cdot O(1)) \allowbreak= O(n)
& O(n) + O(n) + (O(n) \cdot O(n)) + (O(n) \cdot O(n)) \allowbreak= O(n^2)
& O(1) + O(1) + (O(n) \cdot O(1)) + (O(n) \cdot O(1)) \allowbreak= O(n) \\ \addlinespace
\multicolumn{3}{@{}l}{Listar mensagens de contacto. (FU + ItM)}\\
O(1) + O(n) = O(n)
& O(n) + O(n) = O(n)
& O(1) + O(n) = O(n) \\ \addlinespace
\multicolumn{3}{@{}l}{Listar mensagens de grupo. (FG + ItM)}\\
O(1) + O(n) = O(n)
& O(n) + O(n) = O(n)
& O(1) + O(n) = O(n) \\
\bottomrule
\end{tabularx}
\smallskip
FU = Find User, IC = Insert Contact, RC = Remove Contact, ItC = Iterate Contacts, FG = Find Group, IP = Insert Participant, RP = Remove Participant, ItP = Iterate Participants, IM = Insert Message, ItG = Iterate Groups, ItM = Iterate Messages
\end{table}
\end{document}
答案4
\documentclass[10pt,a4paper]{article}
\begin{document}
\footnotesize \noindent\hspace{-2cm}
\begin{tabular}{|l|l|l|l|}
\hline
\multicolumn{1}{|c|}{\textbf{Método}} &
\multicolumn{1}{c|}{\textbf{Melhor caso}} &
\multicolumn{1}{c|}{\textbf{Pior caso}} &
\multicolumn{1}{c|}{\textbf{Caso esperado}} \\ \hline
Inserir novo utilizador &
O(1) &
O(n) &
O(1 + \textbackslash{}lambda) \\ \hline
Consulta dados de utilizador &
O(1) &
O(n) &
O(1 + \textbackslash{}lambda) \\ \hline
Inserir contacto &
\begin{tabular}[c]{@{}l@{}}O(1) + O(1) = O(1)\\ Find User + Insert Contact\end{tabular} &
\begin{tabular}[c]{@{}l@{}}O(n) + O(n) = O(n)\\ Find User + Insert Contact\end{tabular} &
\begin{tabular}[c]{@{}l@{}}O(1) + O(log(n)) = O(log(n))\\ Find User + Insert Contact\end{tabular} \\ \hline
Inserir novo grupo &
O(1) &
O(n) &
O(1 + \textbackslash{}lambda) \\ \hline
Consulta dados de grupo &
O(1) &
O(n) &
O(1 + \textbackslash{}lambda) \\ \hline
Remover grupo &
O(1) &
O(n) &
O(1+ \textbackslash{}lambda) \\ \hline
\end{tabular}
\end{document}